0
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?

4
Contributors
5
Replies
7
Views
8 Years
Discussion Span
Last Post by yinchao
0

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?

0

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

Thanks for the fast reply.

0

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 by vegaseat: rules

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.