Linked Questions

1 vote
3 answers
3k views

I have a function reference that is being called by an object. What I'd like is for that object to pass itself as a parameter to the function as it would normally do for its own functions. IE I would ...
Alter's user avatar
  • 3,494
2 votes
3 answers
671 views

Let us suppose the following situation: I have a class with some initial values. Furthermore, I want to provide the possibility to pass an user-defined method, when initializing a new object. The user ...
Slango's user avatar
  • 55
0 votes
0 answers
1k views

I have added my decorator to instance methods of the class: def method_decorator(func): def wrap(self, *args, **kwargs): print("Start to perform " + func.__name__) try: ...
Gleb's user avatar
  • 93
0 votes
2 answers
80 views

Problem Description I want to use a decorator to define a class method, but this requires me to manually give the 'self' object when I shouldn't have to provide that. def func_wrapper(func): ...
Taw's user avatar
  • 429
0 votes
0 answers
107 views

I have a class: class A(object): def __init__(self): self._first=1 def a(self): pass I want to dynamically add a method to it, but I want the name to be based on input (in my ...
81 votes
6 answers
47k views

I'm asking this question because of a discussion on the comment thread of this answer. I'm 90% of the way to getting my head round it. In [1]: class A(object): # class named 'A' ...: def f1(...
24 votes
4 answers
12k views

Based on my understanding of Python's data model, and specifically the subsection "Instance Methods", whenever you read an attribute whose value is of type "user-defined function", some magic kicks in ...
Joe White's user avatar
  • 98.4k
10 votes
3 answers
15k views

Is it possible to implement generic method handlers in python which allow for calling of non-existent functions? Something like this: class FooBar: def __generic__method__handler__(.., methodName, ....
paweloque's user avatar
  • 19k
22 votes
4 answers
9k views

I have an entry point function call it main on an object that I would like to remain unmocked, since it calls several other methods on the object: class Thing(object): def main(self): ...
14 votes
4 answers
4k views

Consider this example of a strategy pattern in Python (adapted from the example here). In this case the alternate strategy is a function. class StrategyExample(object): def __init__(self, ...
8 votes
3 answers
979 views

Or is everything a method? Since everything is an object, a def whatever: is just a method of that file.py, right?
TIMEX's user avatar
  • 275k
18 votes
4 answers
2k views

Why does the __get__ method in a python descriptor accept the owner class as it's third argument? Can you give an example of it's use? The first argument (self) is self evident, the second (instances)...
Finn's user avatar
  • 1,873
5 votes
2 answers
14k views

I am trying to import all the objects from a subfolder into a class in python 3.8, and am strugguling to find a way to do so. I don't want to manually have to import all the objects, as there are far ...
5 votes
3 answers
8k views

Say there is: class A(B): ... where B could be object and ... is not: @classmethod # or @staticmethod def c(cls): print 'Hello from c!' What do I have to do that calling A.c() wont trigger ...
6 votes
4 answers
3k views

In the interest of reusing some existing code that was defined as an instance method of a different class, I was tying to do something like the following: class Foo(object): def __init__(self): ...

15 30 50 per page
1
2 3