Having Trouble with Scoping Rules

Fredrik Lundh fredrik at pythonware.com
Tue Jan 31 06:40:56 CET 2006


Charles Krug wrote:

> > def ExpensiveObject():
> >      global _expensiveObject
> >      if not(_expensiveObject):
> >          _expensiveObject = "A VERY Expensive object"
> >
> >      return _expensiveObject
> >
> > The documentation will no doubtedly explain it better than I have

> Okay, that works in the module where I define the function.  But if I
> import the module:
>
> # expensive Object User
> import Expensive
>
> print Expensive.ExpensiveObject()
>
> I get the same exception.

not if you followed Farshid's instructions.

> Okay THIS seems to be working for me:
>
> # expensive Object Module
>
> _expensiveObject = None
> def ExpensiveObject():
>     try:
>         retval = _expensiveObject
>     except UnboundLocalError:
>         _expensiveObject = "A VERY Expensive object"
>         retval = _expensiveObject
>
>     return retval

that doesn't work at all:

    _expensiveObject = None
    def ExpensiveObject():
        try:
            retval = _expensiveObject
        except UnboundLocalError:
            _expensiveObject = "A VERY Expensive object"
            print "CREATED VERY EXPENSIVE OBJECT"
            retval = _expensiveObject
        return retval

    if __name__ == "__main__":
        print _expensiveObject
        print ExpensiveObject()
        print ExpensiveObject()
        print ExpensiveObject()


prints

    None
    CREATED VERY EXPENSIVE OBJECT
    A VERY Expensive object
    CREATED VERY EXPENSIVE OBJECT
    A VERY Expensive object
    CREATED VERY EXPENSIVE OBJECT
    A VERY Expensive object

compare this to Farshid's solution:

    _expensiveObject = None

    def ExpensiveObject():
        global _expensiveObject
        if _expensiveObject is None:
            _expensiveObject = "A VERY Expensive object"
            print "CREATED VERY EXPENSIVE OBJECT"
        return _expensiveObject

    if __name__ == "__main__":
        print _expensiveObject
        print ExpensiveObject()
        print ExpensiveObject()
        print ExpensiveObject()

which prints

    None
    CREATED VERY EXPENSIVE OBJECT
    A VERY Expensive object
    A VERY Expensive object
    A VERY Expensive object

and works perfectly fine if you import it from another module:

    >>> import test
    >>> print test.ExpensiveObject()
    CREATED VERY EXPENSIVE OBJECT
    A VERY Expensive object
    >>> print test.ExpensiveObject()
    A VERY Expensive object
    >>> print test.ExpensiveObject()
    A VERY Expensive object

> Which gives me:
>
> >>> import Expensive
> >>> a = Expensive.ExpensiveObject()
> >>> b = Expensive.ExpensiveObject()
> >>> a == b
> True
> >>> a is b
> True
> >>>

the only thing you've proved here is that the string constant "A
VERY Expensive object" is the same thing as the string constant
"A VERY Expensive object".

> I'll try it with my actual class instance to verify.  Anyone see
> anything I'm missing?

</F>






More information about the Python-list mailing list