On 21 Jun., 22:51, Scott David Daniels <scott.dani...@acm.org> wrote: > LorenzoDiGregoriowrote: > > On 21 Jun., 01:54, Dave Angel <da...@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...@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. -- http://mail.python.org/mailman/listinfo/python-list