funcdict = {
'mypackage.mymodule.myfunction': mypackage.mymodule.myfunction,
....
}
funcdictmyvar
Answer from Ignacio Vazquez-Abrams on Stack Overflow Top answer 1 of 8
132
funcdict = {
'mypackage.mymodule.myfunction': mypackage.mymodule.myfunction,
....
}
funcdictmyvar
2 of 8
48
It's much nicer to be able to just store the function itself, since they're first-class objects in python.
import mypackage
myfunc = mypackage.mymodule.myfunction
myfunc(parameter1, parameter2)
But, if you have to import the package dynamically, then you can achieve this through:
mypackage = __import__('mypackage')
mymodule = getattr(mypackage, 'mymodule')
myfunction = getattr(mymodule, 'myfunction')
myfunction(parameter1, parameter2)
Bear in mind however, that all of that work applies to whatever scope you're currently in. If you don't persist them somehow, you can't count on them staying around if you leave the local scope.
Address: 31-38 Queen Street Hull HU1 1UU United Kingdom
You can use pointers in python?
The * is not used for pointers in Python, but for packing and unpacking of collections. In an argument list, *args means that any additional positional arguments are collected in a tuple named args: def test(a, b, *args): print(a, b, args) test(1, 2, 3, 4, 5) 1 2 (3, 4, 5) Likewise, **kwargs means that any additional key-word arguments are collected in a dictionary named kwargs: def test(a, b, **kwargs): print(a, b, kwargs) test(a=1, b=2, c=3, d=4, e=5) 1 2 {'c': 3, 'd': 4, 'e': 5} By the way, the parameter list you posted works a little strangely in that form. The second positional argument would assign to verbose and only any positional arguments beyond that would go into args. It should be written like this: def runcmd(cmd, *args, verbose=False, **kwargs): Then verbose is a key-word only argument, as was probably intended. More on reddit.com
How to pass a python function to a C function as a function pointer in Cython
Hi all, I am writing a cython class like this. cdef class Button: cdef CButton* ptr cdef object _onClick def __cinit__(self, ...): self.ptr = make_btn_ptr_in_C() @onClick.setter def onClick(self, pfunc): self._onClick = pfunc setClickHandler(self.ptr, self._onClick) This setClickHandler ... More on discuss.python.org
Let python call a C function pointer passed from the C Python API
I have C++ a void function pointer stored in a variable call. The pointed function takes an int and a char* as arguments. I have a python module containing this function: def yeah(x): x(int(0),"text argument") return "pointer called" As you can see I'm trying to use the argument x of the... More on thecodingforums.com
C Python API: os.register_at_fork analogue with C function pointer arguments - Ideas - Discussions on Python.org
Rather than have to take a C function, make a python wrapper for it, use the c/python api to get the function pointer to os.register_at_fork(), and call it with the C functionโs python wrapper as an argument, I propose adding a new PyOS_RegisterAtFork c/python ... More on discuss.python.org
06:40
#54 Python Tutorial for Beginners | Lambda or Anonymous Function ...
03:28
function pointers in python - YouTube
08:51
Pointers in Python Are A Real Thing - YouTube
python pass pointer to function
17:02
Introducing Pointers in Python using the Ctypes Library - YouTube
01:11
PYTHON : Python function pointer - YouTube
GeeksforGeeks
geeksforgeeks.org โบ python โบ turning-a-function-pointer-to-callable
Turning a Function Pointer to Callable - Python
April 2, 2019 - from llvm.core import Module, Function, Type, Builder mod = Module.new('example') f = Function.new(mod, Type.function( Type.double(), [Type.double(), Type.double()], False), 'foo') block = f.append_basic_block('entry') builder = Builder.new(block) x2 = builder.fmul(f.args[0], f.args[0]) y2 = builder.fmul(f.args[1], f.args[1]) r = builder.fadd(x2, y2) builder.ret(r) Output : <llvm.core.Instruction object at 0x10078e990> Code #5 : ... from llvm.ee import ExecutionEngine engine = ExecutionEngine.new(mod) ptr = engine.get_pointer_to_function(f) ptr Output :
Reddit
reddit.com โบ r/learnpython โบ you can use pointers in python?
r/learnpython on Reddit: You can use pointers in python?
April 26, 2022 -
I just saw this as a function definition and was curious?
def runcmd(cmd, verbose = False, *args, **kwargs):
Top answer 1 of 6
21
The * is not used for pointers in Python, but for packing and unpacking of collections. In an argument list, *args means that any additional positional arguments are collected in a tuple named args: def test(a, b, *args): print(a, b, args) test(1, 2, 3, 4, 5) 1 2 (3, 4, 5) Likewise, **kwargs means that any additional key-word arguments are collected in a dictionary named kwargs: def test(a, b, **kwargs): print(a, b, kwargs) test(a=1, b=2, c=3, d=4, e=5) 1 2 {'c': 3, 'd': 4, 'e': 5} By the way, the parameter list you posted works a little strangely in that form. The second positional argument would assign to verbose and only any positional arguments beyond that would go into args. It should be written like this: def runcmd(cmd, *args, verbose=False, **kwargs): Then verbose is a key-word only argument, as was probably intended.
2 of 6
7
In python the * syntax doesn't indicate a pointer but a list of arbitrary positional arguments . ** is similar but for key word arguments.
Google Groups
groups.google.com โบ g โบ cython-users โบ c โบ 0ouYUUa60R4
passing a pointer function to another function which can be called in python
September 24, 2017 - In that case, ars.normal would already be a wrapper of the function pointer, so in that case you'd need to write your function that is supposed to receive the function to accept a wrapper of that type and you'd have to figure out a way to access the pointer. Note that, if you go this route, your type-safety is entirely based on your ars.normal.argtypes definition. You should probably declare the return type somewhere as well. ... Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message ... Sorry again for my posts here: I ended up writing the following python function (wrapper) in order to get access to my "initial" and "sample" C-type functions: def py_ars(int ns, int m, double emax,
Bobby Hadz
bobbyhadz.com โบ blog โบ using-function-pointers-in-python
How to use Function Pointers in Python [4 Ways] | bobbyhadz
April 11, 2024 - The function pointer string is stored in a variable. We called the eval() function with the string and it resolved to the actual greet function from the another module. The second set of parentheses is used to invoke the greet function. However, notice that we still had to import the another module even though it's not directly used in our code. The argument we passed to the eval() function is parsed and evaluated as a Python expression.
Python.org
discuss.python.org โบ python help
How to pass a python function to a C function as a function pointer in Cython - Python Help - Discussions on Python.org
February 7, 2024 - Hi all, I am writing a cython class like this. cdef class Button: cdef CButton* ptr cdef object _onClick def __cinit__(self, ...): self.ptr = make_btn_ptr_in_C() @onClick.setter def onClick(self, pfunc): self._onClick = pfunc setClickHandler(self.ptr, self._onClick) This setClickHandler ...
The Coding Forums
thecodingforums.com โบ archive โบ archive โบ python
Let python call a C function pointer passed from the C Python API | Python | Coding Forums
November 16, 2009 - Click to expand... Python can't call C function pointers. You have to write a function in C that accepts a CObject and some arguments, unpacks the arguments, retrieves the function pointer from the CObject, and calls it. Quick and dirty function that might do that (untested, not robust).
Real Python
realpython.com โบ pointers-in-python
Pointers in Python: What's the Point? โ Real Python
March 18, 2026 - Because you defined add_one() in this shared object, you can access it as if it were any other Python object. Before you call the function though, you should specify the function signature. This helps Python ensure that you pass the right type to the function. In this case, the function signature is a pointer to an integer.
Quora
quora.com โบ How-do-I-create-pointers-in-Python
How to create pointers in Python - Quora
Answer (1 of 7): It is not necessary, as labels will function as references to values automatically. Python restricts references to always point to some value. You cannot make a reference without an initializing value, even if it is [code ]None[/code]. Python functions return [code ]None[/code] b...
Learn C++
learncpp.com โบ cpp-tutorial โบ function-pointers
20.1 โ Function Pointers โ Learn C++
August 8, 2007 - In lesson 12.7 -- Introduction to pointers, you learned that a pointer is a variable that holds the address of another variable. Function pointers are similar, except that instead of pointing to variables, they point to functions!
GeeksforGeeks
geeksforgeeks.org โบ python โบ using-pointers-in-python-using-ctypes
Using Pointers in Python using ctypes - GeeksforGeeks
July 23, 2025 - As the name suggests void pointer ... into any type and can hold data of any type. Since in Python the concept of pointers is not properly available or used we can't properly create a Void Pointer, as the POINTER function requires one argument i.e the data type....
GeeksforGeeks
geeksforgeeks.org โบ python โบ python-functions
Python Functions - GeeksforGeeks
Mutable objects like lists can be modified inside functions. Immutable objects like integers and strings remain unchanged. ... def myFun(x): x[0] = 20 b = [10, 11, 12, 13] myFun(b) print(b) def myFun2(x): x = 20 a = 10 myFun2(a) print(a) ... myFun(x) modifies the first element of the list and lists are mutable, so the original list changes. myFun2(x) assigns a new value to x. Integers are immutable, so the original value of a remains unchanged. Note: Python uses pass-by-object-reference, where functions receive references to objects instead of actual copies.
Published: 2 weeks ago
Wikipedia
en.wikipedia.org โบ wiki โบ Function_pointer
Function pointer - Wikipedia
July 27, 2026 - A function pointer, also called a subroutine pointer or procedure pointer, is a pointer referencing executable code, rather than data. Dereferencing the function pointer yields the referenced function, which can be invoked and passed arguments just as in a normal function call.
Python
python-list.python.narkive.com โบ stqBvePE โบ calling-c-function-pointers-from-python
Calling C Function Pointers from Python
Interesting -- it should be possible ... be that easy, since you would need to find a way to call a function pointer with a variable number of arguments in C. Look at the 'dl' module in the standard Python distribution....
Filipekberg
filipekberg.se โบ 2009 โบ 11 โบ 30 โบ function-pointers-in-c-and-python-and-their-possible-usage
Function pointers in C and Python and their possible usage
May 31, 2021 - Imagine the scenario where you might want to performe some calculation but you don't know what type of API the end user which will end up using your API have. Let's say that the method "square" ( x^x ) would not be defined. So you might end up having to use function pointers, where you ask the user to suppy the function for calculating square of x. In Python the Square function for x^x will look like this
Python.org
discuss.python.org โบ ideas
C Python API: os.register_at_fork analogue with C function pointer arguments - Ideas - Discussions on Python.org
March 3, 2023 - Python can register callbacks using os.register_at_fork() to occur surrounding forks. The c/python api documents functions like PyOS_BeforeFork(), but does not provide a way to register functions implemented in C to occuโฆ
Cprogramming
cprogramming.com โบ tutorial โบ function-pointers.html
Function Pointers in C and C++ - Cprogramming.com
How to begin Get the book ยท C tutorial C++ tutorial Game programming Graphics programming Algorithms More tutorials