Ric*_*ick 1 python django import class
我想在类中使用导入,然后由另一个类继承,这样我就不必在每个文件中手动定义导入.我正在尝试这样但它不起作用,任何建议表示赞赏:
class Djangoimports ():
def __init__(self):
from django.template import Context
print Context
class Init1 (Djangoimports):
def __init__(self):
Djangoimports.__init__(self)
self.c = Context(self.constructor_dict) # just example of trying to use the imported "Context"
>>>>> global name 'Context' is not defined
Run Code Online (Sandbox Code Playgroud)
我尝试过尝试使用"self"的变体,但无法弄清楚如何通过导入来适当地使用它,因为它与我通常使用'self'的类属性/方法不同
这对我来说很好.
但是你最好这样做:
>>> class Test(object):
... from functools import partial
...
>>> Test().partial
<type 'functools.partial'>
Run Code Online (Sandbox Code Playgroud)
请注意,按照您的方式执行,您必须在每个实例的基础上初始化它们并分配给self,如下所示:
def Test(object):
def __init__(self):
from functools import partial
self.partial = partial
Run Code Online (Sandbox Code Playgroud)
无论哪种方式,您现在可以访问bar
该类的其他方法或派生的方法self.bar
.