Static Variables in Python?
Cliff Wells
cliff at develix.com
Mon Jul 31 16:43:55 EDT 2006
On Mon, 2006-07-31 at 13:37 -0700, Cliff Wells wrote:
> On Mon, 2006-07-31 at 13:02 -0700, Cliff Wells wrote:
>
>
> > @attrs ( bits = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ] )
>
> Also, IMO, it's a bit more readable to write:
>
> bits = [ 0 for i in range ( 16 ) ]
Or even:
bits = [ 0 ] * 16
Just be careful to only use that style when the contents of the array
are non-mutable. The list comp does the right thing in that case (at
risk of going on a tangent):
Right:
>>> bits = [ { } for i in range ( 16 ) ]
>>> bits
[{}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}]
>>> bits [ 0 ][ 'a' ] = 1
>>> bits
[{'a': 1}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}]
Wrong:
>>> bits = [ {} ] * 16
>>> bits [ 0 ][ 'a' ] = 1
>>> bits
[{'a': 1}, {'a': 1}, {'a': 1}, {'a': 1}, {'a': 1}, {'a': 1}, {'a': 1},
{'a': 1}, {'a': 1}, {'a': 1}, {'a': 1}, {'a': 1}, {'a': 1}, {'a': 1},
{'a': 1}, {'a': 1}]
>>>
Regards,
Cliff
More information about the Python-list
mailing list