Lorenzo Di Gregorio wrote:
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
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to