Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Instantiate all functions in Python

Tags:

python

Is there a way to do something like this:

util.py contains:
def add
def subtract

instantiate.py contains:
def instantiate

where instantiate does:

import util
def instantiate():
    add = util.add
    subtract = util.subtract

So I can skip typing util everytime I use a function and I can instantiate them all using one function?

I tried but I get

NameError: global name 'util' is not defined
like image 481
J Roq Avatar asked Aug 02 '26 14:08

J Roq


2 Answers

You can import specific functions from a module :

from util import add, substract
like image 168
Vincent Savard Avatar answered Aug 04 '26 07:08

Vincent Savard


if you just want to import those functions into the same namespace, you could do something like this:

from util import *

then you can write add and so on without prefixing it with that module

like image 36
bjarneh Avatar answered Aug 04 '26 06:08

bjarneh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!