Need help with Python scoping rules

Jan Kaliszewski zuo at chopin.edu.pl
Wed Aug 26 22:48:01 CEST 2009


26-08-2009 o 09:03:27 Ulrich Eckhardt <eckhardt at satorlaser.com> wrote:

> Jean-Michel Pichavant wrote:
>> class Color:
>>     def __init__(self, r, g,b):
>>           pass
>>     BLACK = Color(0,0,0)
>>
>> It make sens from a design point of view to put BLACK in the Color
>> namespace. But I don't think it's possible with python.
>
> class Color:
>     ...
>
> setattrib(Color, "BLACK", Color(0,0,0))

Or simpler:

     class Color:
         ...

     Color.BLACK = Color(...)

Then
(Color.BLACK is Color.BLACK.BLACK.BLACK.BLACK) == True
:-)

*j

PS. Obviously, that's nothing special (there is no problem with
creating such "recursive" references in Python).

-- 
Jan Kaliszewski (zuo) <zuo at chopin.edu.pl>



More information about the Python-list mailing list