Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Object-Oriented Python (retired) Inheritance Intro to Inheritance

igsm '
igsm '
10,440 Points

Where the newly defined attributes are stored?

Kenneth Love

Hey! Formally talking, each new object has its own environment, which inherits class's default attributes or initialize the local attributes.

In the case with the 'setattr', when we define a new attribute, where it is stored after and how we can access it or keep a track of it? I hope I put my question in the clear way. If I think abstractly, then I imagine that the new attributes, which were set on fly, hang somewhere in the space (but definitely in the space of an environment of the new instance). The solution I might think of is creating a storage manually (in the form of the list or dictionary) where we place whatever new was defined.

Can somebody explain it please from the scientific point of view? Thank you.

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

Attributes are tracked in an object's "namespace". "Class" Attributes are defined as part of the class namespace. "Instance" attributes are defined as part of the individual instance namespace. They behave like python dictionaries. An attribute is first looked for in the instance namespace, then the class namespace. You can list the contents of the namespaces using dir(some_object)

An example:

def MyClass():
    class_var = 1

# See class variable
MyClass.class_var
1

# See class namespace:
dir(MyClass)
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__',
 '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__',
 '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__',
 '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__',
 'class_var']


# Create instance
my1 = MyClass()

# See instance namespace (currently similar to class)
dir(my1)
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__',
 '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__',
 '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__',
 '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__',
 'class_var']

# See class_var value
my1.class_var
1

# Set class_var locally (becomes instance var)
my1.class_var = "thing"
my1.class_var
'thing'
MyClass.class_var
1

# Create new instance
my2 = MyClass()
my2.class_var
1

# Change class variable
MyClass.class_var = "Whoa!"
MyClass.class_var
'Whoa!'

# ready for this?
my2.class_var
'Whoa!'  # <-- class attribute changes with class
my1.class_var
'thing' # <--still local instance attribute

# delete local instance variable
del my1.class_var
# what is it now?
my1.class_var
'Whoa!' # <-- falls back to the class attribute
igsm '
igsm '
10,440 Points

Awesome! Thank you.