How object's setattr differs from inherited?

C

Ciantic

class MyObject(object):
.... pass
....Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'object' object has no attribute 'myvar'

Why simple inheritance from object does not cause errors at setattr,
yet direct init of object() does?


I was trying to "decor" objects, lists etc with own attributes that
could be used in templates and was sadly not permitted to do so:
something = [1,2]
something.myvar = 'value'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'list' object has no attribute 'myvar'

(I could "solve" this by inheriting from list, but there are cases
where I can't do so, the data is coming elsewhere and wrapping it is
ugly, adding new attribute would be neater)

But my question now is why this setattr problem happens on some types?
What are the types that usually does not allow to arbitrary setattr?
 
T

Thomas Rachel

Am 29.07.2011 17:12 schrieb Ciantic:
... pass
...Traceback (most recent call last):
File "<stdin>", line 1, in<module>
AttributeError: 'object' object has no attribute 'myvar'

Why simple inheritance from object does not cause errors at setattr,
yet direct init of object() does?


I was trying to "decor" objects, lists etc with own attributes that
could be used in templates and was sadly not permitted to do so:
something = [1,2]
something.myvar = 'value'
Traceback (most recent call last):
File "<stdin>", line 1, in<module>
AttributeError: 'list' object has no attribute 'myvar'

(I could "solve" this by inheriting from list, but there are cases
where I can't do so, the data is coming elsewhere and wrapping it is
ugly, adding new attribute would be neater)

But my question now is why this setattr problem happens on some types?
What are the types that usually does not allow to arbitrary setattr?
['__class__', '__delattr__', '__doc__', '__format__',
'__getattribute__', '__hash__', '__init__', '__new__', '__reduce__',
'__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__',
'__subclasshook__']['__class__', '__delattr__', '__dict__', '__doc__', '__format__',
'__getattribute__', '__hash__', '__init__', '__module__', '__new__',
'__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__',
'__str__', '__subclasshook__', '__weakref__']


What are the differences?

['__dict__', '__module__', '__weakref__']

As the attributes are stored in __dict__, I would suppose that it is
__dict__ which makes the difference. object() has no such and therefore
there is no place to save an attribute.


Thomas
 
T

Thomas Jollans

... pass
...Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'object' object has no attribute 'myvar'

Why simple inheritance from object does not cause errors at setattr,
yet direct init of object() does?


I was trying to "decor" objects, lists etc with own attributes that
could be used in templates and was sadly not permitted to do so:
something = [1,2]
something.myvar = 'value'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'list' object has no attribute 'myvar'

(I could "solve" this by inheriting from list, but there are cases
where I can't do so, the data is coming elsewhere and wrapping it is
ugly, adding new attribute would be neater)

But my question now is why this setattr problem happens on some types?
What are the types that usually does not allow to arbitrary setattr?

object has pre-defined slots (instead of just storing everything is a
__dict__) -- so can your objects:

Python 3.2.1 (default, Jul 11 2011, 12:37:49)
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information..... __slots__ = ['a', 'b']
....Traceback (most recent call last):


- Thomas
 
A

Andrew Berg

... pass
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'object' object has no attribute 'myvar'

Why simple inheritance from object does not cause errors at setattr,
yet direct init of object() does?
object objects have no __dict__. User-defined objects typically do. Each
child of MyObject would likely have __dict__ as well.
I was trying to "decor" objects, lists etc with own attributes that
could be used in templates and was sadly not permitted to do so:
something = [1,2]
something.myvar = 'value'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'list' object has no attribute 'myvar'
Lists have no __dict__, and AFAIK, there's no reason they should.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,782
Latest member
ThomasGex

Latest Threads

Top