5

I have following simplified class named Password.py in folder1:

import random                                                                    

CHARS = "ABC"

class PasswordHelper(object):                                                  
    @staticmethod                                                                          
    def generateChars(length):                                                    
       return ''.join(random.choice(CHARS) for x in range(length))

Now I have another class TestClass.py in folder2:

sys.path.append('../folder1/')                              
import Password                                         

class Tester:                                           
    def whatever(self):                                   
        print Password.generateChars(3)

def main():
     x = Tester()
     x.whatever()

# call main method
main()

When calling python TestClass.py I get the following error: AttributeError: 'module' object has no attribute 'generateChars'. Both folders are on the same level. Is there a problem with the way I import the class files or with the static method declaration itself?

2 Answers 2

9

Python is not Java.

Firstly, there is absolutely no point to either the Tester or the Password classes. If you're not storing state, then don't define a class. Make both whatever and generateChars into normal standalone functions.

However, assuming you're doing this just to learn about Python classes, you have not understood that a class does not equal a module in Python. Since you've imported the Password module, you still need to refer to the PasswordHelper class:

Password.PasswordHelper.generateChars(3)

Alternatively, you can import the PasswordHelper class:

from Password import PasswordHelper
...
PasswordHelper.generateChars(3)

Finally, please follow PEP8 for your module, class and function names.

Sign up to request clarification or add additional context in comments.

4 Comments

+1 NB even if you have state it may make sense to pass that state around as arguments and return values. But that's a matter of taste, first get rid of the "classes for everything" mindset.
Hmm. Alright. I seriously thought that having a class for everything was considered good style. I guess I was wrong...
Actually I'm wondering, why exactly it is bad to have classes for everything? Are there more "deeper" readings to it?
@cherrun You should read Python is not Java, it also raises other issues. My personal problem with classes for everything is that it's plain pointless: It just adds another element to the picture which doesn't have any use, but wastes screen space, mental capacity, needs a name (which has to be remembered), etc. -- and it indicates that the programmer is unaware of paradigms beside OOP and their use (or, even worse, shuns them for bad/invalid reasons).
2

You defined the function in a class, so you need to reference it with the classname too:

print Password.PasswordHelper.generateChars(3)

Alternatively, move the function out of the class definition, at which point you do not need to use @staticmethod at all:

import random                                                                    

CHARS = "ABC"

def generateChars(length):                                                    
    return ''.join(random.choice(CHARS) for x in range(length))

In Python, functions do not have to be part of a class definition.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.