Access from class variable to one on constructor

Steven D'Aprano steven at
Mon Feb 23 23:50:18 EST 2009

On Mon, 23 Feb 2009 08:02:17 -0200, Gabriel Genellina wrote:

> En Mon, 23 Feb 2009 07:40:47 -0200, Kless <jonas.esp at>
> escribió:
>> How to access from a class variable to one that is initialized on the
>> constructor?
>> --------------
>> class Foo():
>>   foo = bar  # I want to access *from here* to variables created on
>> the constructor.
>>   def __init__(self, bar_init):
>> = bar_init
>> --------------
> Unless I misunderstand you, you can't do that. Class variables are
> shared by all instances and don't depend on a particular instance - they
> exist even *before* any instance is created.

Please don't use the term "class variables" to mean class *attributes*, 
it makes my brain hurt. If a string variable is a string, and an int 
variable is an int, and a bool variable is a bool, a class variable 
should be a class.

What you can do is shift the creation of the class attribute to the 
constructor, instead of when the class is created:

class Spam():
    def __init__(self, bar_init): = bar_init

But I'm not sure why you would want to.


More information about the Python-list mailing list