Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8856173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:17:59+00:00 2026-06-14T14:17:59+00:00

Suppose I have a class NamedObject which has an attribute name . Now if

  • 0

Suppose I have a class NamedObject which has an attribute name. Now if I had to use a setter, I would first have to define a getter (I guess?) like so:

class NamedObject:
    def __init__(self, name):
        self.name = name

    @property
    def name(self):
        return self._name

Now I was wondering, inside the setter, should I use self._name or self.name, the getter or the actual attribute? When setting the name, I ofc. need to use _name, but what about when I’m getting INSIDE the setter? For example:

@name.setter
def name(self, value):
    if self._name != str(value): # Or should I do 'if self.name != value' ?
        self.doStuff(self._name) # Or doStuff(self.name) ?
        self.doMoreStuff()
        self._name = str(value)

Does it actually matter which one to use, and why use one over the other?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T14:18:00+00:00Added an answer on June 14, 2026 at 2:18 pm

    If your getter has significant logic (like lazy initialization), then you should access through the getter all the time.

    class Something(object):
        UNINITIALIZED = object()
        LAZY_ATTRS = ('x','y','z')
        def __init__(self):
            for attr in self.LAZY_ATTRS:
                setattr(self, '_'+attr, self.UNINITIALIZED)
        def _get_x(self):
            if self._x is self.UNINITIALIZED:
                self._x = self.doExpensiveInitStuff('x')
            return self._x
    

    But if all your getter does is return self._x, just access the internal variable directly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have some class which has a property actor_ of type Actor .
Suppose I have a class Base which has a member variable A* my_hash. I
Suppose I have a class DisposableObject which implements IDisposable. There is a risk it
Suppose I have a class MyClass to which I want to add certain 'observer'
Suppose i have class Person { public int Id {get;set;} public string Name {get;set;}
Suppose I have class D which inherits from class B . and I have
Suppose I have a model: class Question < ActiveRecord::Base attr_accessible :title # it has
Suppose i have : class Library(models.Model): name = models.CharField(max_length = 100) class Books(models.Model): library
Suppose I have class Foo(db.Model): bar = db.ReferenceProperty(Bar) foo = Foo.all().get() Is there a
Suppose I have: class Foo { ... }; class Bar : public Foo {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.