__module__ / scoping question

Andrew Bennetts andrew-pythonlist at puzzling.org
Mon Feb 24 01:55:09 EST 2003


On Mon, Feb 24, 2003 at 06:47:02AM +0000, Clark C. Evans wrote:
> I'd like to do something like...
> 
> class foo:
>     def __init__(self,x):
>         self.x = x
> class bar:
>     def foo(self):
>         return __module__.foo(self)
> 
> So that the "foo" method of the "bar" object
> returns a "foo" object.  Yes, I could rename
> the outer "foo" to something else... but any
> other ideas?

How about:
    >>> class foo:
    ...     def __init__(self,x):
    ...         self.x = x
    ... 
    >>> class bar:
    ...     def foo(self):
    ...         global foo
    ...         return foo(self)
    ... 
    >>> bar().foo()
    <__main__.foo instance at 0x8110364>
    >>> _.x
    <__main__.bar instance at 0x810f914>

-Andrew.






More information about the Python-list mailing list