Python 3.1, object, and setattr()

E

Ethan Furman

Greetings!

Perhaps I woke up too early this morning, but this behaviour has me baffled:

Python 3.1.1 (r311:74483, Aug 17 2009, 17:02:12) [MSC v.1500 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.

--> test = object()

--> setattr(test, 'example', 123)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'object' object has no attribute 'example'

Shouldn't setattr() be creating the 'example' attribute? Any tips
greatly appreciated!

~Ethan~
 
S

Steve Howell

Greetings!

Perhaps I woke up too early this morning, but this behaviour has me baffled:

Python 3.1.1 (r311:74483, Aug 17 2009, 17:02:12) [MSC v.1500 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.

--> test = object()

--> setattr(test, 'example', 123)
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
AttributeError: 'object' object has no attribute 'example'

Shouldn't setattr() be creating the 'example' attribute?  Any tips
greatly appreciated!

On 2.6.2 the error seems to be limited to instances of object. If you
subclass object, you are fine. I do not know why that is so; I'm just
verifying that the behavior you see is not limited to 3.1.1.

Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41)
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more
information. Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'object' object has no attribute 'example'

It's probably good to subclass object anyway, with something like:

class Record(object):
pass

But I do not know your use case.
 
T

Terry Reedy

On 2.6.2 the error seems to be limited to instances of object. If you
subclass object, you are fine. I do not know why that is so;

As the other Steve said, object is a built-in class; user-defined
subclasses of object are user-defined classes. (And all other built-in
classes are built-in subclasses of object, at least in 3.x.)

Python objects can have either a fixed or variable set of attributes. By
default, instances of user classes have an attribute dictionary
(__dict__) for a variable set of attributes.
{'a': 3}

The exception is when '__slots__ = xxx' is part of the class definition.
It instances then have a fixed set of attributes.
Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
c.a = 1
AttributeError: 'C' object has no attribute 'a'

The error message means that 'a' is not in the fixed set of instance
attributes (as defined by __slots__) for class C.

In general, instances of built-in classes do not have such attribute
dictionaries and one their attribute set is fixed (and often empty). It
is as if built-in classes have '__slots__ = xxx' as part of their
definition. In particular, object and others act like they have
'__slots__ = ()', which means they have no instance attributes.

There are two exceptions among built-in classes that I know of: type and
'function', (whose instances are user-defined functions).
{} # standard dict just as with user class instances

Instances of type are more complicated:
3

so the dict_proxy for user classes is writable

butTraceback (most recent call last):
File "<pyshell#18>", line 1, in <module>
int.z = 3
TypeError: can't set attributes of built-in/extension type 'int'

wheres for builtins, it is not.

Terry Jan Reedy
 

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,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top