0

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?

Johan
  • 74,508
  • 24
  • 191
  • 319
user2869231
  • 1,431
  • 5
  • 24
  • 53
  • These are functions. If they were inside a class, that would make them also be methods. – tripleee Oct 11 '13 at 01:17
  • they are inside a class, though? – user2869231 Oct 11 '13 at 01:20
  • What’s the error you get when you try a global function? – Josh Lee Oct 11 '13 at 01:22
  • well I don't think I'm doing it right. It expects a tab if I put it on the same line as the class within the class, so I tried putting it outside of the class. When I do that, it says the variables used in it were referenced before created. Therefore, I moved the variables needed inside of it (also global) and now it just doesn't like the idea of iterating a list through it... – user2869231 Oct 11 '13 at 01:31
  • Why are you defining `doMath` and `square` inside of a class though? Unlike Java, for instance, you don't need to put everything in a class in Python, and if you're not going to use any of the object-oriented language features, you shouldn't. – Blckknght Oct 11 '13 at 01:36
  • This was just to imitate what I wrote. The code I have does need to be a class, I just added "class" in here just in case it made some kind of difference with the error – user2869231 Oct 11 '13 at 01:47

4 Answers4

1

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)
Crowman
  • 25,242
  • 5
  • 48
  • 56
  • so you created an object of the entire class and used that? Didn't really think about that... for a couple other situations I put some methods in completely different classes to do that lol should I have self an literally every method in the program? – user2869231 Oct 11 '13 at 01:40
  • You should have `self` in every method that belongs to a class and is intended to attach to an instance. You don't need a `self` for static methods, and you don't need to use classes at all where it doesn't make sense to. If you find your functions don't need a `self` (or a `cls`, for class methods), then generally speaking, they should be standalone methods, and not in a class at all, since by definition they don't operate on any class or instance data. – Crowman Oct 11 '13 at 01:41
  • Ok makes sense. Can it ever hurt anything to put self in all the methods for the heck of it? (Thank you by the way) – user2869231 Oct 11 '13 at 01:46
  • It's not that it hurts, but it would be pretty meaningless to put `self` on methods that were outside of the class, since there would be no instance to be the `self`. For regular methods (i.e. not class methods or static methods) that belong to a class, you *have* to have a `self` parameter, or you'll get errors when you call them via an instance. – Crowman Oct 11 '13 at 01:47
  • that is very good to know because I have like... 14 methods, and none of them have self in it... I just made it more difficult on myself by putting some of them in a different class so I could reference it through there – user2869231 Oct 11 '13 at 01:49
  • As a general rule, if you don't see any concrete reason to put something in a class, then don't put it in a class. Classes are tools, and you should use them when it makes sense to do so, not just because you feel it's expected of you. The default should usually be writing standalone functions unless it makes specific sense to write a class. – Crowman Oct 11 '13 at 01:52
1

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

Community
  • 1
  • 1
Johan
  • 74,508
  • 24
  • 191
  • 319
0

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
Ben
  • 683
  • 3
  • 5
0
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()
cure
  • 2,588
  • 1
  • 17
  • 25