Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct use of static methods

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?

like image 270
cherrun Avatar asked Nov 03 '12 17:11

cherrun


People also ask

What is true about static methods?

A static method can only access static data members and static methods of another class or same class but cannot access non-static methods and variables. Also, a static method can rewrite the values of any static data member.

What are the rules for static methods?

Characteristics of Static MethodsA static method can call only other static methods; it cannot call a non-static method. A static method can be called directly from the class, without having to create an instance of the class. A static method can only access static variables; it cannot access instance variables.

What is the correct way to call a static method?

To call a static method from another class, you use the name of the class followed by the method name, like this: ClassName. methodName().

What is use of static why it is useful?

It can be used with variables, methods, blocks and nested classes. It is a keyword which is used to share the same variable or method of a given class. Basically, static is used for a constant variable or a method that is same for every instance of a class. The main method of a class is generally labeled static.


2 Answers

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.

like image 24
Martijn Pieters Avatar answered Sep 27 '22 03:09

Martijn Pieters


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.

like image 186
Daniel Roseman Avatar answered Sep 27 '22 03:09

Daniel Roseman