End of file

Bengt Richter bokr at oz.net
Mon Oct 11 00:51:28 EDT 2004


On Mon, 11 Oct 2004 16:22:28 +1300, Greg Ewing <greg at cosc.canterbury.ac.nz> wrote:

>Alex Martelli wrote:
>> Since an immediate instance of type object has no possible use except as
>> a unique, distinguishable placeholder,
>
>That's not true -- you can also assign attributes to such
>an object and use it as a record. (It's not a common use,
>but it's a *possible* use!)
>
I originally thought that too, but (is this a 2.3.2 bug?):

 Python 2.3.2 (#49, Oct  2 2003, 20:02:00) [MSC v.1200 32 bit (Intel)] on win32
 Type "help", "copyright", "credits" or "license" for more information.
 >>> obj = object()
 >>> obj.x = 1
 Traceback (most recent call last):
   File "<stdin>", line 1, in ?
 AttributeError: 'object' object has no attribute 'x'
 >>> dir(obj)
 ['__class__', '__delattr__', '__doc__', '__getattribute__', '__hash__', '__init__', '__new__', '
 __reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__']
 >>> type(obj)
 <type 'object'>
 >>> obj
 <object object at 0x008DE3B8>

__setattr__ is there, so how does one make it do something useful?


OTOH, making a thing to hang attributes on is a one liner (though if you
want more than one instance, class Record: pass; rec=Record() is probably better.
<BTW>
why is class apparently not legal as a simple statement terminated by ';' ?
(I wanted to attempt an alternate one-liner ;-)

 >>> class Record:pass; rec=Record()
 ...
 Traceback (most recent call last):
   File "<stdin>", line 1, in ?
   File "<stdin>", line 1, in Record
 NameError: name 'Record' is not defined
 >>> class Record:pass
 ...
 >>> rec=Record()
 </BTW>

 >>> rec = type('Record',(),{})()
 >>> rec.x=1
 >>> vars(rec)
 {'x': 1}
 >>> type(rec)
 <class '__main__.Record'>
 >>> rec
 <__main__.Record object at 0x00901110>

Regards,
Bengt Richter



More information about the Python-list mailing list