简体   繁体   中英

Python: Calling Methods from a Method

I've been trying to make a soccer game using Python. Most of the problems I've run into I've been able to find a way around. However, I'm getting stuck on the error "global name '---' not defined", referring to a method name. From what I've seen, the problem deals with trying to call a method from a method. Since pasting my code would take too much time explaining what was going on, I wrote a very basic example of the problem:

class example():

    def doMath(x,y):
        z = x + y
        z = square(z)
        return z

    def square(z):
        z = z * z
        return z

    print doMath(5,4)

This is in no way meant to be a program worth writing, nor is it the smart way of doing that calculation... It just imitates the problem I'm having. This code will not work, because the method "doMath" doesn't know the method "square" exists. I've seen this fixed by making the square method a submethod (I don't know what it's called, it's just indented under the primary method). However, that is not viable in my soccer code since I'd be having multiple methods calling it. I've seen similar questions to this, but the answers still don't fit my code. A global function seems like it would be what I'm looking for, but it typically leads to an error of something not existing. I could just add a bunch of instructions to the main, but that's alot of work and lines of code that I'd prefer not to have to add - it would make it pretty ugly.

So the main question is... how can I get the doMath method to call the square method without having to combine them.

While we're at it... I've been calling these methods rather than functions... am I correct on that?

As others have noted, you need to use self , and you need to call your methods correctly, with an instance, for example:

#!/usr/bin/python   

class example(): 
    def doMath(self, x, y):
        z = x + y
        z = self.square(z)
        return z

    def square(self, z):
        z = z * z
        return z

p = example()
print p.doMath(5,4)

outputs:

paul@local:~/src/python$ ./square.py
81
paul@local:~/src/python$

Clearly, in this particular case there's no advantage to these methods being in a class at all, and you could more easily do:

#!/usr/bin/python

def square(z):
    return z * z

def doMath(x, y):
    return square(x + y)

print doMath(5,4)

While we're at it... I've been calling these methods rather than functions... am I correct on that?

method -> routine that is a member of a class.
function -> routine that returns a result (compare with mathematical function)
procedure -> routine that does not return a result
member -> part of a class or struct (either a member variable or a member function etc)

Procedures are odd in python because even though a procedure does not return anything you can still assign its result to a variable.
The result in this case is None see here: Python procedure return values

If doMath and square are part of a class, they both should have another parameter called self . Methods calls take place on this self parameter. For example:

def doMath(self, x, y):
    z = x + y
    z = self.square(z)
    return z
class example():

    def doMath(self,x,y):
        z = x + y
        z = self.square(z)
        return z

    def square(self,z):
        z = z * z
        return z
    def p(self):
        print self.doMath(5,4)
e=example()
e.p()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM