How to automate accessor definition?
kj
no.email at please.post
Sun Mar 21 13:25:08 EDT 2010
In <4ba58503$0$27838$c3e8da3 at news.astraweb.com> Steven D'Aprano <steve at REMOVE-THIS-cybersource.com.au> writes:
>On Sat, 20 Mar 2010 22:15:54 +0000, kj wrote:
>> I need to create a class solely for the purpose of encapsulating a large
>> number of disparate data items.
>There's a built-in for that. It's called "dict". Syntax for item access
>is a tiny bit different, but still very common:
>data['foo']
>instead of
>data.foo
I find the latter more readable than the former. All those extra
elements (the brackets and the quotes, vs the single dot) add
Perl-like visual noise to the code, IMHO.
And dicts are vulnerable to this sort of bug:
data['typo'] = type(foobar)
Also, AFAIK, initialization of a dictionary is never as simple as
i = myclass(*fields)
But in a sense you're right: aside from these objections,
*functionality-wise* what I'm looking for is not very different
from a dictionary, or a C struct.
>> At the moment I have no plans for any
>> methods for this class other than the bazillion accessors required to
>> access these various instance variables.
>Huh? If you have instance variables, why don't you refer to them by name?
I'm sorry, I used the wrong terminology. I see now that the correct
term is "(instance) attribute", not "instance variable".
>Leave the repetitive accessor code out. Python isn't Java.
>http://dirtsimple.org/2004/12/python-is-not-java.html
Thanks for the link! The bit about "Guido's time machine" is pretty
funny.
~K
More information about the Python-list
mailing list