Re: Frustrated with scopes
James Stroud wrote: def stream_factory: class Line(object): __source = source __join = join # etc. return Line of course I meant def stream_factory(lines, source, join=''.join): James -- http://mail.python.org/mailman/listinfo/python-list
Re: Frustrated with scopes
andrew cooke wrote: On Aug 12, 7:49 am, andrew cooke and...@acooke.org wrote: On Aug 12, 1:51 am, James Stroud nospamjstroudmap...@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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Frustrated with scopes
On Wed, 12 Aug 2009 04:49:06 -0700, andrew cooke wrote: 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...). James asked you to describe the behaviour you expect. Please explain what you expect, and what you actually get. Post the ACTUAL error message, not a summary, not a paraphrase, but an actual copy and paste. In any case, your code snippet works for me: class _StreamFactory(object): ... @staticmethod ... def __call__(lines, source, join=''.join): ... class Line(object): ... __source = source ... __join = join ... obj = _StreamFactory() obj(['a', 'b'], ab) No errors. Of course it doesn't return anything, because your code snippet doesn't return anything either. Here's a modified version which returns the inner class: class _StreamFactory2(object): ... @staticmethod ... def __call__(lines, source, join=''.join): ... class Line(object): ... __source = source ... __join = join ... return Line ... obj = _StreamFactory2() K = obj(['a', 'b'], ab) K class '__main__.Line' K._Line__source 'ab' Works perfectly. I suspect your error is probably something like you have misspelled source somewhere. -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: Frustrated with scopes
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Frustrated with scopes
On Aug 12, 8:52 am, Dave Angel da...@ieee.org wrote: 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 Ah! OK, thanks for that. I need to look at this again. I'll post again if necessary, but if it works for you then I clearly don't understand what the issue is myself. Andrew -- http://mail.python.org/mailman/listinfo/python-list
Frustrated with scopes
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Frustrated with scopes
correction: source and join are undefined. Sorry, Andrew -- http://mail.python.org/mailman/listinfo/python-list
Re: Frustrated with scopes
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. I assume you will return the newly created Line class when you call an instance of _StreamFactory? There may be some things about the above that you might be overlooking: 1. __call__ does not supersede the __init__ constructor of _StreamFactory. 2. __source and __join are name-mangled in later versions of python. They will be attributes of the returned Line class named _Line_source and _Line_join respectively. Rather than make _StreamFactory a class, you will probably get the behavior you desire if you simply make it a function: def stream_factory: class Line(object): __source = source __join = join # etc. return Line And then don't forget that double underscores produce name mangling and you'll be set. James -- http://mail.python.org/mailman/listinfo/python-list