开发者

Instantiate all functions in Python

开发者 https://www.devze.com 2023-03-31 18:04 出处:网络
Is there开发者_JAVA百科 a way to do something like this: util.py contains: def add def subtract instantiate.py contains:

Is there开发者_JAVA百科 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


You can import specific functions from a module :

from util import add, substract


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


If you want a method that adds/subtracts, you should use operator.add or operator.sub.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号