Linked Questions
35 questions linked to/from How to bind an unbound method without calling it?
1
vote
3
answers
3k
views
python pass self to referenced function [duplicate]
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
Creating an object and passing an user-defined function as method [duplicate]
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 ...
0
votes
0
answers
1k
views
How to call function without decorator in Python 3 [duplicate]
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:
...
0
votes
2
answers
80
views
decorating methods causes method to pass in objects [duplicate]
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):
...
0
votes
0
answers
107
views
setattr not passing self to methods added to class [duplicate]
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
What is the difference between a function, an unbound method and a bound method?
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
Assigning a function to an object attribute
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 ...
10
votes
3
answers
15k
views
Generic methods in python
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, ....
22
votes
4
answers
9k
views
How do I patch an object so that all methods are mocked except one?
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
How does a python method automatically receive 'self' as the first argument?
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
Are there functions in Python, or is everything a method?
Or is everything a method?
Since everything is an object, a
def whatever:
is just a method of that file.py, right?
18
votes
4
answers
2k
views
Why does a python descriptor __get__ method accept the owner class as an arg?
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
Import all functions and classes inside a module into a class python
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
Python dynamic class methods
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 Python, can you call an instance method of class A, but pass in an instance of class B?
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):
...