Frustrated with scopes

Diez B. Roggisch deets at nospam.web.de
Wed Aug 12 08:12:14 EDT 2009


andrew cooke wrote:

> On Aug 12, 7:49 am, andrew cooke <and... at acooke.org> wrote:
>> On Aug 12, 1:51 am, James Stroud <nospamjstroudmap... at mbi.ucla.edu>
>> wrote:
>>
>>
>>
>> > andrew cooke wrote:
>> > > Is there a way to make this work (currently scope and join are
>> > > undefined at runtime when the inner class attributes are defined):
>>
>> > > class _StreamFactory(object):
>>
>> > > @staticmethod
>> > > def __call__(lines, source, join=''.join):
>>
>> > > class Line(object):
>>
>> > > __source = source
>> > > __join = join
>> > > [...]
>>
>> > It would be helpful if you were to describe the type of behavior you
>> > expect.
>>
>> Sorry, I didn't make myself clear.  When run the code gives
>> NameError: name 'source' is not defined
>> because the class namespace blocks the function namespace (or
>> something...).
> 
> ie when the __call__ method is invoked on an instance of
> _StreamFactory.

But you can refer to the arguments if you don't insist on setting them as
class-attributes:




def factory(foo, bar):

    class A(object):

        def do_something(self):
            print foo, bar

    return A()

a = factory(10, 20)
a.do_something()



Diez



More information about the Python-list mailing list