Frustrated with scopes

Dave Angel davea at ieee.org
Wed Aug 12 14:52:50 CEST 2009


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
> [...]
>
> I can get something working by bouncing through global values, but it
> looks awful and I think it's a source of a bug due too values being
> redefined.
>
> Thanks,
> Andrew
>
>   
Supply us with just enough source code to actually try it, give the full 
error message including traceback, and tell us what you expected to 
see.  Also tell us Python version   (sys.version)

So far you've done none of these.  When I try the following, I get no 
errors, using Python 2.6.2


class _StreamFactory(object):

    @staticmethod
    def __call__(lines, source, join=''.join):

        class Line(object):

            __source = source
            __join = join
        return Line()


fact = _StreamFactory()
obj = fact(43, "name.txt")
print obj





More information about the Python-list mailing list