class classA:
#saved as classA.py    
   def __init__(self):
      print "class A"
      printB()
   
   def printB():
         print "B"
>>> classA()
class A
Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    classA()
  File "C:\Documents and Settings\Owner\Desktop\timetable\classA.py", line 5, in __init__
    printB()
NameError: global name 'printB' is not defined

Using the class above, I get the error saying that printB is not defined. It works when printB() is moved outside of the class. I want to have printB to be part of the class, like a static method. I tried adding "@staticmethod" above the 'def printB()' but that doesn't seem to work. Am I missing something?

Thanks for the link, I'm looking at it now.

class classA:
#saved as classA.py    
   def __init__(self):
      print "class A"
      classA.printB()
      
   @staticmethod
   def printB():
      print "B"

classA()

printB() does not rely on the instance, it would be better as a static method. This code works, but I need to do 'classA.' first. Is there any way to omit calling the class since the method is already in the class?

Oh, I'll just add 'self' to the method then.

Thanks for the fast reply.

Traceback (most recent call last):
File
Traceback (most recent call last):
File "C:/Users/Web/Desktop/velocity.py", line 9, in <module>
main()
File "C:/Users/Web/Desktop/velocity.py", line 2, in main
angle, vel, h0, tiime = getInputs()
NameError: global name 'getInputs' is not defined

Note: Please post your own new thread and show the code that causes the error.

Edited 6 Years Ago by vegaseat: rules

This question has already been answered. Start a new discussion instead.