Can global variable be passed into Python function?

Michael Torrie torriem at gmail.com
Sat Mar 1 12:05:37 EST 2014


On 03/01/2014 04:28 AM, Marko Rauhamaa wrote:
> Ben Finney <ben+python at benfinney.id.au>:
> 
>> Use ‘==’, since that's all that matters for getting a value that will
>> work fine.
> 
> You are telling me to use '==' if I choose string objects and 'is' if I
> choose some other objects.

No, '==' works fine no matter what objects you assign to your state
variables.

class Foo(object):
    STATE1 = object()
    STATE2 = "testing"
    STATE3 = 2

    def __init__(self):
        self.state = Foo.STATE1

    def bar(self):
        if self.state == Foo.STATE1:
            pass
        elif self.state == Foo.STATE2:
            pass
        elif self.state == Foo.STATE3:
            pass

> I prefer a solution that works regardless of what objects I choose for
> identifiers.

As shown, '==' does work for this too.  I don't know which is more
correct, but it does work.




More information about the Python-list mailing list