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
classes vs dicts
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="Daniel 'Dang' Griffith, post: 1759211"] Yes, you're right. I mis-spoke what I meant. Maybe this is closer, "You can't add or remove arbitrary attributes to an instance of a class... without magic." What I mean is that with a dictionary, the keys can come from external/arbitrary data and be added to the dictionary with a simple assignment: d = dict() k, v = raw_input(), raw_input() d[k] = v w = d[k] You can't do that to classes or instances without some kind of magic. class Foo: pass foo = Foo() k, v = raw_input(), raw_input() setattr(foo, k, v) w = getattr(foo, k) Maybe its just me, but I consider getattr/setattr magic (although in this simple example, they don't appear too magical, so maybe others wouldn't consider it so). Still, I think it is an overgeneralization to say that classes are just syntactic sugar for dictionaries. --dang [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
Python
classes vs dicts
Top