Can global variable be passed into Python function?

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sat Mar 1 01:58:34 CET 2014


On Sat, 01 Mar 2014 02:11:53 +0200, Marko Rauhamaa wrote:

> Ben Finney <ben+python at benfinney.id.au>:
> 
>> As has been pointed out to you, the whole point here is that string
>> objects often *are not* distinct, despite conceptually having distinct
>> cretion in the source.
> 
> You know full well that this initialization creates references to
> distinct objects:
> 
>     class ABC:
>         IDLE = "IDLE"
>         CONNECTING = "CONNECTING"
>         CONNECTED = "CONNECTED"
>         DISCONNECTING = "DISCONNECTING"
>         DISCONNECTED = "DISCONNECTED"
> 
> The 5 constants can (and should) be distinguished with the "is"
> operator. Using "==" in this case would be slightly misleading.

I disagree. You are focusing on identity, but identity is (usually) not 
important. That's an implementation detail.

What we have here is the curse of the singleton design anti-pattern:

http://accu.org/index.php/journals/337

https://molecularmusings.wordpress.com/2011/11/11/singleton-is-an-anti-pattern/

Since the symbols IDLE, CONNECTING etc. don't have state apart from their 
name, whether there is one or a million and one instances is irrelevant.


-- 
Steven



More information about the Python-list mailing list