Python-ese for indexing a list within a class

Larry Whitley ldw at us.ibm.com
Wed Jul 19 15:23:00 EDT 2000


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





More information about the Python-list mailing list