Procedures

Dave Angel davea at ieee.org
Mon Jun 22 19:22:51 EDT 2009


Greg Reyna wrote:
> <div class="moz-text-flowed" style="font-family: -moz-fixed">Learning 
> Python (on a Mac), with the massive help of Mark Lutz's excellent 
> book, "Learning Python".
>
> What I want to do is this:
> I've got a Class Object that begins with a def.  It's designed to be 
> fed a string that looks like this:
>
> "scene 1, pnl 1, 3+8, pnl 2, 1+12, pnl 3, 12, pnl 4, 2+4,"
>
> I'm parsing the string by finding the commas, and pulling out the data 
> between them.
> No problem so far (I think...)  The trouble is, there is a place where 
> code is repeated:
>
> 1. Resetting the start & end position and finding the next comma in 
> the string.
>
> In my previous experience (with a non-OOP language), I could create a 
> 'procedure', which was a separate function.  With a call like: 
> var=CallProcedure(arg1,arg2) the flow control would go to the 
> procedure, run, then Return back to the main function.
>
> In Python, when I create a second def in the same file as the first it 
> receives a "undefined" error.  I can't figure out how to deal with 
> this.  How do I set it up to have my function #1 call my function #2, 
> and return?
>
> The only programming experience I've had where I pretty much knew what 
> I was doing was with ARexx on the Amiga, a language much like Python 
> without the OOP part.  ARexx had a single-step debugger as part of the 
> language installation.  I've always depended on a debugger to help me 
> understand what I'm doing (eg Script Debugger for Apple Script--not 
> that I understand Apple Script)  Python's debug system is largely 
> confusing to me, but of course I'll keep at it.  I would love to see a 
> step-by-step debugging tutorial designed for someone like me who 
> usually wants to single-step through an entire script.
>
> Thanks for any help,
> Greg Reyna
>
>
You should post an example.  Otherwise we can just make wild guesses.

So for a wild guess, perhaps your question is how an instance method 
calls another instance method.  But to put it briefly, a def inside a 
class definition does not create a name at global scope, but instead 
defines a method of that class.  Normally, you have to use an object of 
that class as a prefix to call such a function  (with the exception of 
__init__() for one example).

class  A(object):
    def  func1(self, parm1, parm2):
           do some work
           self.func2(arg1)
           do some more work
    def func2(self, parm1):
            do some common work

q = A()
q.func1("data1", "data2")

Here we use q.func1()  to call the func1 method on the q instance of the 
class.   We could also call q.func2() similarly.  But I think you may 
have been asking about func1 calling func2.  Notice the use of 
self.func2().  Self refers to the object of the method we're already in.






More information about the Python-list mailing list