Can global variable be passed into Python function?
Steven D'Aprano
steve at pearwood.info
Fri Feb 28 03:23:53 EST 2014
On Fri, 28 Feb 2014 09:43:58 +0200, Marko Rauhamaa wrote:
> Chris Angelico <rosuav at gmail.com>:
>
>> Simple rule of thumb: Never use 'is' with strings or ints. They're
>> immutable, their identities should be their values. Playing with 'is'
>> will only confuse you, unless you're specifically going for
>> introspection and such.
>
> Here's a use case for "is" with strings (or ints):
I don't think this is a use-case for "is". See below.
> class Connection:
> IDLE = "IDLE"
[...]
> CONNECTED = "CONNECTED"
[...]
> def disconnect(self):
> ...
> if self.state is CONNECTED:
> ...
Why do you care that the state is *that specific* string, rather than any
old string with the value "CONNECTED"?
Unless you can explain a good reason why, say, *this* instance
"CONNECTED" should fail the test, while *that* instance with the same
value passes, it's not a good use-case for "is".
--
Steven
More information about the Python-list
mailing list