static-methods

How to have Static and Method Functions with the same name

How to have Static and Method Functions with the same name Question: I’m trying to create a class for interacting with a MySQL Database. This class will connect to the database storing the connection object and cursor. Allowing for multiple actions without reconnecting every time. Once you’re done with it, the cursor and connection need …

Total answers: 1

When should a static method be a function?

When should a static method be a function? Question: I am writing a class for an image processing algorithm which has some methods, and notably a few static methods. My IDE keeps telling me to convert static methods to function which leads me to the following question: When should a static method be turned into …

Total answers: 1

Python – Unable to call static method inside another static method

Unable to call static method inside another static method Question: I have a class which has static methods and I want to have another static method within this class to call the method but it returns NameError: name ”method_name’ is not defined Example of what I’m trying to do. class abc(): @staticmethod def method1(): print(‘print …

Total answers: 2

super() and @staticmethod interaction

super() and @staticmethod interaction Question: Is super() not meant to be used with staticmethods? When I try something like class First(object): @staticmethod def getlist(): return [‘first’] class Second(First): @staticmethod def getlist(): l = super(Second).getlist() l.append(‘second’) return l a = Second.getlist() print a I get the following error Traceback (most recent call last): File “asdf.py”, line …

Total answers: 4

How can I dynamically create class methods for a class in python

How can I dynamically create class methods for a class in python Question: If I define a little python program as class a(): def _func(self): return "asdf" # Not sure what to resplace __init__ with so that a.func will return asdf def __init__(self, *args, **kwargs): setattr(self, ‘func’, classmethod(self._func)) if __name__ == "__main__": a.func I receive …

Total answers: 6

Why use classmethod instead of staticmethod?

Why use classmethod instead of staticmethod? Question: I know what they do and I’ve seen many examples of both, but I haven’t found a single example where I would have to use classmethod instead of replacing it with a staticmethod. The most common example of classmethod I’ve seen is for creating a new instance of …

Total answers: 3

Calling class staticmethod within the class body?

Calling class staticmethod within the class body? Question: When I attempt to use a static method from within the body of the class, and define the static method using the built-in staticmethod function as a decorator, like this: class Klass(object): @staticmethod # use as decorator def _stat_func(): return 42 _ANS = _stat_func() # call the …

Total answers: 6

Meaning of @classmethod and @staticmethod for beginner?

Meaning of @classmethod and @staticmethod for beginner Question: What do @classmethod and @staticmethod mean in Python, and how are they different? When should I use them, why should I use them, and how should I use them? As far as I understand, @classmethod tells a class that it’s a method which should be inherited into …

Total answers: 12

Calling static method in python

Calling static method in python Question: I have a class Person and a static method in that class called call_person: class Person: def call_person(): print “hello person” In the python console I import the class Person and call Person.call_person(). But it is giving me error that says ‘module’ object has no attribute ‘call_person’. Can anyone …

Total answers: 5