Defining a code object from a function
Jeremy Hylton
jeremy at digicool.com
Thu Apr 5 15:31:24 EDT 2001
>>>>> "NB" == Neil Benn <neil.benn at cambridgeantibody.com> writes:
NB> Hello,
NB> I have a class as follows:-
NB> class plate:
NB> NoColumns = 0
NB> NoRows = 0
NB> currentRow = 0
NB> def __init__(self):
NB> print "creating plate"
NB> print "created plate"
NB> def hasMoreRows(self):
NB> if currentRow < noRows:
NB> return 1ist
NB> else:
NB> return 0
NB> def nextRow(self):
NB> self.currentRow = self.currentRow + 1
NB> return self.currentRow
NB> I'm using the inspect module to get information about the
NB> method
NB> arguments back.
NB> However...................
NB> My problem is [...]
It looks like you have other problems :-). The hasMoreRows() method
will fail with a NameError because you use a bunch of unbound names.
I think you need to change it to:
def hasMoreRows(self):
if self.currentRow < self.noRows:
return 1
else:
return 0
You're also initializing NoColumns, NoRows, and currentRow as class
attributes. This will probably work correctly in your application,
because all of the attributes you are using are immutable objects.
The first assignment to, e.g. self.NoRows, will create an instance
attribute self.NoRows. But if you used a list or some other mutable
object, you'd have to be more careful. It is sometime better to
initialize instance variable in the __init__():
def __init__(self):
self.currentRow = 0
self.NoRows = 0
self.NoColumns = 0
self.listAttr = []
If you are clear on the distinction between class attributes and
instance attributes, then I apologize for being pedantic. But it's
easy to get confused and the hasMoreRows() method suggests that you're
new to Python.
Jeremy
More information about the Python-list
mailing list