Linked Questions
10 questions linked to/from Same name for classmethod and instancemethod
0
votes
0
answers
51
views
static method called instaed of instance method [duplicate]
I have a class like this:
class Eggs:
def __init__(self):
pass
def Spam(self):
print "spamming object"
@staticmethod
def Spam():
print "spamming class"
The ...
0
votes
0
answers
31
views
Can class().method() and class.method() do different things? [duplicate]
I'd like to know if it's possible to have two methods in a class with the same name (or only one if it can do what I want) but the first method would be called on class().method() and the second one ...
27
votes
5
answers
9k
views
How can I detect duplicate method names in a Python class?
When writing unit tests, I sometimes cut and paste a test and don't remember to change the method name. This results in overwriting the previous test, effectively hiding it and preventing it from ...
10
votes
1
answer
2k
views
python: hybrid between regular method and classmethod
sometimes I have need to write class with static methods,
however with possibility to initialized it and keep state (object)
sth like:
class A:
@classmethod
def method(cls_or_self):
# get ...
11
votes
2
answers
2k
views
Can the staticmethod and classmethod decoraters be implemented in pure Python?
This is an academic question, more than a practical one. I'm trying to delve into the foundations of Python, and I wonder: could I implement the staticmethod and classmethod decorators in pure Python ...
1
vote
1
answer
96
views
Make method can be called from class or instance
I want to make a method to be called from class or instance.
For example :
class SomeClass:
a = 10
def __init__(self, val):
self.a = val
def print_a(self):
print(self.a)
...
1
vote
1
answer
84
views
Access `self` in classmethod when instance (self) calls classmethod
Is it possible to access the object instance in a Python @classmethod annotated function when the call occurs via the instance itself rather than the class?
class Foo(object):
def __init__(self, ...
2
votes
1
answer
71
views
Call instance or class method, whichever is appropriate
This question mentions a hack to differentiate between an instance method (passing self) and a staticmethod (passing nothing):
class X:
def id(self=None):
if self is None:
# It's ...
2
votes
0
answers
67
views
one function as a class and also instance method
This could be a so called XY problem, so let me start with what I want:
ed = Edit.add(1).add(2).add(3)
print(ed.op) # [1,2,3]
Following is what I tried and basically I got working code, but I'm ...
2
votes
1
answer
68
views
How to pass any object as a classmethod's first argument - circumvent injection of class argument
Passing a class, or a totally different object, as the first argument to method is easy:
class Foo:
def method(self): ...
Foo.method(object()) # pass anything to self
I wonder, is this possible ...