class level properties
Peter Otten
__peter__ at web.de
Sun Apr 13 16:34:19 EDT 2008
Charles D Hixson wrote:
> Peter Otten wrote:
>> Charles D Hixson wrote:
>>
>>
>>> I want a hundred or so read-only variables, and I'm not sure the best
>>> way to achieve it.
>>>
>>
>> What do you really want to do? I recommend that you forget about bondage
>> and rely upon displine:
>>
>> class Test(object):
>> """Never change an attribute with an uppercase name."""
>> SIMPLE = "simple example working"
>>
>> Now that was easy...
>>
>> Peter
>>
>>
> What I'm doing it translating Java code which has a large number of
> "public static final (type)" variables.
Ah, Java, the class is an artefact of the language then, and my example
becomes
SIMPLE = "simple example working"
> As to your answer ... yes, and with good discipline you can write object
> oriented code in C and never need a garbage collector. It's *not* a
> good answer. Before I'd chose that one, I'd make it necessary to
Hmm, if you were to choose between a Java dialect without garbage collection
or without the 'final' keyword, would you throw a coin?
> instantiate the class before testing the value of it's constants. It's
> just that that seems to be a silly requirement, so I'd like to avoid
Silly or not, it keeps your code simpler, and simplicity just cannot be
overvalued.
> it. (That's the "solution" that I currently have working with
> __getattr__.)
I'm confident that after you have been coding in Python for a while the
Javaisms will wither away. For now, if you feel that uppercase module-level
names are too big a leap I suggest that you add a __setattr__() method that
records any attempts to modify read-only attributes. That way you'll have a
way to learn whether that particular safety net was a useful investment or
just dead code.
Peter
More information about the Python-list
mailing list