Inheritance and forward references (prototypes)

Scott David Daniels Scott.Daniels at Acm.Org
Sun Jun 21 16:51:58 EDT 2009

Lorenzo Di Gregorio wrote:
> On 21 Jun., 01:54, Dave Angel <da... at> wrote:
>> ...
>> class B(object):
>>     def __init__(self,test=None):
>>         if test==None:
>>             test = A()
>>         self.obj =()
>>         return
> ...
> I had also thought of using "None" (or whatever else) as a marker but
> I was curious to find out whether there are better ways to supply an
> object with standard values as a default argument.
> In this sense, I was looking for problems ;-)
> Of course the observation that "def" is an instruction and no
> declaration changes the situation: I would not have a new object being
> constructed for every instantiation with no optional argument, because
> __init__ gets executed on the instantiation but test=A() gets executed
> on reading 'def'....

If what you are worrying about is having a single default object, you
could do something like this:

     class B(object):
         _default = None

         def __init__(self, test=None):
             if test is None:
                 test = self._default
                 if test is None:
                     B._default = test = A()

--Scott David Daniels
Scott.Daniels at Acm.Org

More information about the Python-list mailing list