Inheritance and forward references (prototypes)
Lorenzo Di Gregorio
lorenzo.digregorio at gmail.com
Mon Jun 22 07:33:43 EDT 2009
On 21 Jun., 22:51, Scott David Daniels <Scott.Dani... at Acm.Org> wrote:
> LorenzoDiGregoriowrote:
> > On 21 Jun., 01:54, Dave Angel <da... at ieee.org> 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.Dani... at Acm.Org- Zitierten Text ausblenden -
>
> - Zitierten Text anzeigen -
Well, I could also declare (ups, define ;-)) __init__(self,**kwargs)
and within the __init__, if kwargs['test'] exists, do test = kwargs
['test'], if it does not exist, do test = A().
The point is that it would have been cleaner to place it straight in
the __init__, but due to the semantic of 'def' this does not seem
possible.
More information about the Python-list
mailing list