Linked Questions

0 votes
0 answers
51 views

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 ...
Woody1193's user avatar
  • 8,202
0 votes
0 answers
31 views

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 ...
Lilian Vault's user avatar
27 votes
5 answers
9k views

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 ...
Scotty Allen's user avatar
  • 13.6k
10 votes
1 answer
2k views

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 ...
Sławomir Lenart's user avatar
11 votes
2 answers
2k views

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 ...
Tom's user avatar
  • 5,251
1 vote
1 answer
96 views

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) ...
Chun-Ye Lu's user avatar
1 vote
1 answer
84 views

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, ...
orange's user avatar
  • 8,252
2 votes
1 answer
71 views

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 ...
karlosss's user avatar
  • 3,225
2 votes
0 answers
67 views

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 ...
VPfB's user avatar
  • 18.1k
2 votes
1 answer
68 views

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 ...
Daraan's user avatar
  • 5,136