static-methods

Python decorator as a staticmethod

Python decorator as a staticmethod Question: I’m trying to write a python class which uses a decorator function that needs information of the instance state. This is working as intended, but if I explicitly make the decorator a staticmetod, I get the following error: Traceback (most recent call last): File “tford.py”, line 1, in <module> …

Total answers: 4

Python: Make class iterable

Python: Make class iterable Question: I have inherited a project with many large classes constituent of nothing but class objects (integers, strings, etc). I’d like to be able to check if an attribute is present without needed to define a list of attributes manually. Is it possible to make a python class iterable itself using …

Total answers: 6

`staticmethod` and `abc.abstractmethod`: Will it blend?

`staticmethod` and `abc.abstractmethod`: Will it blend? Question: In my Python app I want to make a method that is both a staticmethod and an abc.abstractmethod. How do I do this? I tried applying both decorators, but it doesn’t work. If I do this: import abc class C(object): __metaclass__ = abc.ABCMeta @abc.abstractmethod @staticmethod def my_function(): pass …

Total answers: 5

How to get (sub)class name from a static method in Python?

How to get (sub)class name from a static method in Python? Question: If I define: class Bar(object): @staticmethod def bar(): # code pass class Foo(Bar): # code pass Is it possible for a function call Foo.bar() to determine the class name Foo? Asked By: Jean-Pierre Chauvel || Source Answers: Replace the staticmethod with a classmethod. …

Total answers: 3

What is the advantage of using static methods?

What is the purpose of static methods? How do I know when to use one? Question: I ran into unbound method error in python with this code: import random class Sample(object): def drawSample(samplesize, List): sample = random.sample(List, samplesize) return sample Choices=range(100) print(Sample.drawSample(5, Choices)) I was able to fix the problem by adding @staticmethod to the …

Total answers: 10

Static methods – How to call a method from another method?

Static methods – How to call a method from another method? Question: When I have regular methods for calling another method in a class, I have to do this class test: def __init__(self): pass def dosomething(self): print "do something" self.dosomethingelse() def dosomethingelse(self): print "do something else" but when I have static methods I can’t write …

Total answers: 7

Static methods in Python?

Static methods in Python? Question: Can I define a static method which I can call directly on the class instance? e.g., MyClass.the_static_method() Asked By: Joan Venge || Source Answers: Yes, check out the staticmethod decorator: >>> class C: … @staticmethod … def hello(): … print “Hello World” … >>> C.hello() Hello World Answered By: Paolo …

Total answers: 12

Class method differences in Python: bound, unbound and static

Class method differences in Python: bound, unbound and static Question: What is the difference between the following class methods? Is it that one is static and the other is not? class Test(object): def method_one(self): print “Called method_one” def method_two(): print “Called method_two” a_test = Test() a_test.method_one() a_test.method_two() Asked By: Franck Mesirard || Source Answers: When …

Total answers: 13