Keyword arguments - strange behaviour?

Fuzzyman fuzzyman at gmail.com
Tue Dec 28 06:38:09 EST 2004


Steven Bethard wrote:
> Fuzzyman wrote:
> > It wasn't that part of the example that I thought was over complex.
> > (although it's not a 'pattern' I use often). You suggested that if
we
> > had dynamic evaluation of default values, you would have to replace
it
> > with :
> >
> >>>>>class foo(object):
> >>>>>    matcher=re.compile(r'...')
> >>>>>    def __new__(self, bar, baz, matcher=None):
> >>>>>        if matcher is None:
> >>>>>            matcher = self.matcher
> >>>>>        ...
> >>>>>        text = matcher.sub(r'...', text)
> >>>>>        ...
> >
> >
> > Now that I thought was over complex... when all you wanted to do
was
> > put a constant into your default value !
>
> Ahh.  Yeah, the thing above is a bit complex, but it keeps the same
> namespaces -- matcher is only available to foo, not the enclosing
> class/module.  Point taken of course. ;)
>
> Steve

I see. I may be wrong on this... *but* I thought the only time when a
variable defined in the same scope as a function wouldn't be available
in the same namespace is when the function is a global but the variable
isn't ? In which case you deserve a NameError if you try and use the
variable as a default value.......

Are there any other circumstances when they wouldn't be in the same
namespace ?
Regards,

Fuzzyman
http://www.voidspace.org.uk/python/index.shtml




More information about the Python-list mailing list