Python-ese for indexing a list within a class
Harry George
hgg9140 at seanet.com
Wed Jul 19 19:13:57 EDT 2000
How are you choosing to use a vs b ...? I generally need to do this
with named objects so I use a map:
class C:
def __init__(self):
counters={}
def someFunction(self):
# some code
#---discover we need a counter for x---
#---where x is "a", or "b", or whatever---
cnt=self.counters
if not cnt.get(x,0):
cnt[x]=0
cnt[x]=cnt[x]+1
"Larry Whitley" <ldw at us.ibm.com> writes:
> Is the following the appropriate Python-ese for indexing a list within a
> class?
>
> # the following is defined outside of either class or function
> nCounters = 4
> a,b,c,d = range(nCounters)
>
> # now define the class
> class C:
> def __init__(self):
> cnt = [0] * nCounters
> def someFunction(self):
> # some code
> self.cnt[a] = self.cnt[a] + 1 # <<< here's the statement in question
> # some more code
>
> I'm trying to avoid the considerably ugly:
>
> self.cnt[self.a] = self.cnt[self.a] + 1
>
> The code seems to work ok but is this the right/best/prettiest/(etc) way to
> go about it?
>
> Larry
>
>
--
Harry George
hgg9140 at seanet.com
More information about the Python-list
mailing list