Ara Kooser wrote:
> Hello all,
> 
>    I am working on trying to understand classes by creating a
> character generator for a rpg. I know I am doing something silly but I
> am not sure what. When I run the program I and type no when prompted I
> get the following message:
> Traceback (most recent call last):
>   File "/Users/ara/Documents/ct_generator.py", line 10, in <module>
>     class Main:
>   File "/Users/ara/Documents/ct_generator.py", line 68, in Main
>     reroll()
>   File "/Users/ara/Documents/ct_generator.py", line 53, in reroll
>     upp()
> NameError: global name 'upp' is not defined
> 
> I guess it doesn't recognize that I want to call the function upp()
> again. I think I might be using the wrong syntax here. My code is
> below. Thank you any help or guidance.

In Python, if you want to call an object method, you have to do so 
explicitly via the object. Otherwise, it looks for a local/global 
variable of the same name.

For example,

def foo():
   print "I am foo!"

class Foo(object):
   def __init__(self):
       pass

   def foo(self):
       print "I am Foo.foo"

   def display(self):
       self.foo()
       foo()

 >>> x=Foo()
 >>> x.display()
I am Foo.foo
I am foo!


All objects methods are passed the object instance explicitly as their 
first argument. It's conventionally called "self" (similar to the "this" 
pointer in C++ if you're familiar with it).

Let me know if there's something that's not clear.

-- 
~noufal
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to