Fuzzyman wrote:
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 ?

Sorta depends on what you mean by "available in the same namespace":

py> class C(object):
...     def f(matcher=re.compile(r'...')):
...         pass
...
py> class D(object):
...     matcher = re.compile(r'...')
...     def f():
...         pass
...
py> class E(object):
...     class f(object):
...         matcher = re.compile(r'...')
...         def __new__(cls):
...             pass
...
py> c = C()
py> c.matcher
Traceback (most recent call last):
  File "<interactive input>", line 1, in ?
AttributeError: 'C' object has no attribute 'matcher'
py> d = D()
py> d.matcher
<_sre.SRE_Pattern object at 0x01142F20>
py> e = E()
py> e.matcher
Traceback (most recent call last):
  File "<interactive input>", line 1, in ?
AttributeError: 'E' object has no attribute 'matcher'

Now in all three classes, the callable 'f' can access matcher -- it's either in f's namespace or within an enclosing namespace.

However, moving the 'matcher' declaration up one level (as in class D) adds matcher to the enclosing class's namespace. The current code (class C) and my proposed code (class E) do not add 'matcher' to the enclosing class's namespace -- they keep it within f's namespace.

This is what I considered to be 'polluting the namespace' -- adding extra things that aren't used by other parts of the class to the class's namespace. (Of course, this only holds if, as in my code, matcher is only needed within f.)

Does that make my problem clearer?

Steve
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to