Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #70528
| From | Pavel Volkov <sailor@lists.xtsubasa.org> |
|---|---|
| Subject | object().__dict__ |
| Date | 2014-04-23 09:39 +0400 |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.9451.1398231951.18130.python-list@python.org> (permalink) |
There are some basics about Python objects I don't understand. Consider this snippet: >>> class X: pass ... >>> x = X() >>> dir(x) ['__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__'] >>> x.foo = 11 And now I want to make a simple object in a shorter way, without declaring X class: >>> y = object() >>> dir(y) ['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__'] >>> y.foo = 12 Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'object' object has no attribute 'foo' The attribute list is different now and there's no __dict__ and the object does not accept new attributes. Please explain what's going on.
Back to comp.lang.python | Previous | Next — Next in thread | Find similar | Unroll thread
object().__dict__ Pavel Volkov <sailor@lists.xtsubasa.org> - 2014-04-23 09:39 +0400 Re: object().__dict_ Duncan Booth <duncan.booth@invalid.invalid> - 2014-04-23 08:56 +0000
csiph-web