Frustrated with scopes

A

andrew cooke

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
 
J

James Stroud

James said:
> 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
 
A

andrew cooke

andrew said:
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...).

Andrew
 
A

andrew cooke

andrew said:
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.
 
D

Diez B. Roggisch

andrew said:
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
 
S

Steven D'Aprano

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:

.... @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:
.... @staticmethod
.... def __call__(lines, source, join=''.join):
.... class Line(object):
.... __source = source
.... __join = join
.... return Line
....
obj = _StreamFactory2()
K = obj(['a', 'b'], "ab")
K
K._Line__source
'ab'


Works perfectly. I suspect your error is probably something like you have
misspelled "source" somewhere.
 
D

Dave Angel

andrew said:
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
 
D

Dave Angel

andrew said:
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
 
A

andrew cooke

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,201
Messages
2,571,048
Members
47,647
Latest member
NelleMacy9

Latest Threads

Top