method in the definiton of a methond

Z

Zunbeltz Izaola

I've a clas with following methods:

class difracsocket:
def __init__(self,sock=None):
self.Handle = 1
[...]

def cliente(self,orden,respuesta):
[...]

def GetNewHandle(self):
newhandle = self.Handle
newhandle += 1
if newhandle > MAXHANDLE:
newhandle = 1
self.Handle = newhandle
return newhandle

def Move(self, table, angle,handle=1):
orden = instruccion(handle)
respuesta = instruccion
orden.Codigo = 6
Comando = "mvr "
if table == "TTH" or "tth":
Comando += "tth "
elif table == "TH" or "th":
Comando += "th "
Comando += str(angle)
orden.Comando = Comando
self.cliente(orden,respuesta)
return respuesta

I what to change de definiton of Move to something like
def Move(self, table, angle,handle=self.GetNewHandle)


File "comunicacion.py", line 201, in difracsocket
def Move(self,handle=self.GetNewHandle()):
NameError: name 'self' is not defined

Any hit? Can't i invoke methods in the definition of other methods?
Thanks in advance

Zunbeltz
 
Z

Zunbeltz Izaola

Gonçalo Rodrigues said:
The traceback tells it all: self is not defined. self is *local* to
the Move method itself. You can't access it from the outside which is
what you are trying to do, by putting it in the args list for Move.

The following should work

def Move(self,handle = None):
if handle is None:
handle = self.GetNewHandler()
etc,...

With my best regards,
G. Rodrigues

Thanks for the answer. I'm understanding python better every time i
read c.l.p :)

Zunbeltz
 
G

Gonçalo Rodrigues

On 29 Oct 2003 12:22:11 +0100, Zunbeltz Izaola

[text snipped]
I what to change de definiton of Move to something like
def Move(self, table, angle,handle=self.GetNewHandle)


File "comunicacion.py", line 201, in difracsocket
def Move(self,handle=self.GetNewHandle()):
NameError: name 'self' is not defined

The traceback tells it all: self is not defined. self is *local* to
the Move method itself. You can't access it from the outside which is
what you are trying to do, by putting it in the args list for Move.

The following should work

def Move(self,handle = None):
if handle is None:
handle = self.GetNewHandler()
etc,...

With my best regards,
G. Rodrigues
 
T

Terry Reedy

Zunbeltz Izaola said:
I what to change de definiton of Move to something like
def Move(self, table, angle,handle=self.GetNewHandle):

The def header, including default arg expression, is 'executed' to
construct the function and is not part of the body executed when the
constructed function is executed. When the function is created,
'self' is just a name not yet bould to anything. What you want is
....handle = None): followed by
a first line of 'if handle == None: handle = self.GetNewHandle'.

Terry J. Reedy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top