Default method arguments

Fredrik Lundh fredrik at pythonware.com
Wed Nov 16 12:07:24 CET 2005


Duncan Booth wrote:

> What you really want is for the marker to exist only in its own little
> universe, but the code for that is even messier:
>
> class A(object):
>    def __init__(self, n):
>        self.data =n
>    def make_f():
>        marker = object()
>        def f(self, x = _marker):

NameError: global name '_marker' is not defined

>            if x is _marker:
>                x = self.data
>            print x
>        return f
>    f = make_f()
>
>>>> instance = A(6)
>>>> instance.f()
> 6

in another universe, perhaps, but not very far away:

    >>> instance.f.im_func.func_defaults[0]
    <object object at 0x009EC438>

    >>> inspect.getargspec(A.f)
    (['self', 'x'], None, None, (<object object at 0x009EC438>,))

</F> 






More information about the Python-list mailing list