I'm learning by my self Python and I have a mind blowing issue, because I don't understand why is not working. I'm using PyDev and I've download version 2 of Python. I have this code:
class Utils:
@staticmethod
def hello():
print "Hi! I'm the Utils class"
Utils.hello() #Hi! I'm the Utils class
and everything is working fine at this point. But if I import the Utils class and call the static method from another module...
import Utils
Utils.hello()
I get this error:
Traceback (most recent call last):
File "C:\Users\migugonz\Desktop\Docs\Per Folder\WorkSpacePy\Rosalind\src\bioinformatics\stronghold\Pruebas.py", line 40, in <module>
Utils.hello()
AttributeError: 'module' object has no attribute 'hello'
I thing it can't be a big deal, but I've been searching a solution and as long I know this shlould be work.
asked Jan 2, 2014 at 10:53
1 Answer 1
I believe you need to do Utils.Utils.hello()
or import like from Utils import Utils
answered Jan 2, 2014 at 10:54
2 Comments
Duncan
This is why PEP8 suggests that modules should have all lowercase names. If @Hannibaal had used
utils.py
as the name for the module there wouldn't have been scope to confuse the module and class.Hannibaal
For now on my modules will be lowercase. I was confused because in java the name of the file (working with eclipse) is the class itself.
lang-py
hello
a global function in Utils.hello
at the top level of the file.