Emulating C++ coding style

Martijn Faassen faassen at pop.vet.uu.nl
Mon Apr 26 07:44:04 EDT 1999


Donn Cave wrote:
> 
> Martijn Faassen <faassen at pop.vet.uu.nl> writes:
> | A static value can be created like this (besides using a global variable
> | in a module):
> |
> | class Foo:
> |    self.shared = 1
> |
> |    def __init__(self):
> |        print self.shared
> 
> Just a nit-pick on this particular item - I tried that already, a
> couple of days ago, so I know it won't work!  You meant to say,
> 
>   class Foo:
>       shared = 1

Whoops, yes, that makes sense. :) Sorry.

> Now a couple of further observations.  Per the question, yes, that
> variable ("attribute") is accessible in the class scope:
> 
>    print Foo.shared
> 
> As well as in the instance scope, as shown in Martijn's example.

[snip surprise]
> It's not much like C++ here, but it's uncanny how it reeks of Python!
> Namespaces, references!

Indeed. Not having used that class attribute trick often myself, I
wasn't aware of this surprising behavior. I suppose in order to get the
C++ behavior it's best to use a module global variable.

Regards,

Martijn




More information about the Python-list mailing list