By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,587 Members | 1,677 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,587 IT Pros & Developers. It's quick & easy.

Attributes and built-in types

P: n/a
Is it just an implementation limitation that attributes cannot be
assigned to instances of internal types?

---------------------------
x = 4
type(x) <type 'int'> class Test(int): .... pass
.... y = Test(4)
type(y) <class '__main__.Test'> y.someattr = 10
x.someattr = 10

Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: 'int' object has no attribute 'someattr'
---------------------------

When I did a dir(int) there was no __dict__ entry, but a dir(Test)
showed a __dict__entry, which is why (presumably) the attribute
assignment worked for Test but not for int.

Just curious...

Dave
Jul 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
You might find this usefull specifically the stuff on subclassing
built-in types.
http://www.python.org/2.2/descrintro.html

Jul 18 '05 #2

P: n/a
Dave Opstad wrote:
Is it just an implementation limitation that attributes cannot be
assigned to instances of internal types?


No, not "just". Some types have a fixed set of attributes by design,
whereas others allow addition of attributes. There are several reasons
for this design. Performance is one of them; backwards compatibility
another.

Regards,
Martin
Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.