Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
Python
Concise idiom to initialize dictionaries
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="Steven Bethard, post: 1789242"] Just the same[1] =) .... def __getattr__(self, name): .... d = {} .... setattr(self, name, d) .... return d .... def __repr__(self): .... items = self.__dict__.items() .... items.sort() .... return '\n'.join(['%s -> %r' % item for item in items]) .... a -> {1: 99} b -> {2: 42, 3: 11} I believe that __getattr__ works just the same (but to check for yourself, see [URL]http://docs.python.org/ref/attribute-access.html[/URL]). I think what you're thinking of is __getattribute__ which new-style classes offer *in addition* to __getattr__. While __getattr__ is called only if an attribute is not found, __getattribute__ is called unconditionally for every attribute access. Steve [1] modulo my preference for list comprehensions/generator expressions instead of map [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
Python
Concise idiom to initialize dictionaries
Top