类继承从不同的模块python [英] Class inheritance from different modules python

查看:326
本文介绍了类继承从不同的模块python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



模块:〜/

foo.py

 导入栏

class foo:
def test b $ b print(this is a test)

module:〜/ bar.py



  class bar(foo):
def __init __(self):
super b $ b

导入栏时,我收到此错误消息:

  NameError:name'foo'未定义


解决方案

如果您要引用名称在另一个模块中,则必须导入它。

  import foo 

class bar foo.foo):
...


I'm new to python and I have a hard time trying to figuring out how can I inherit from a class in an other module.

module: ~/foo.py

import bar

class foo:
    def test(self)
        print("this is a test")

module: ~/bar.py

class bar(foo):
    def __init__(self):
        super().test()

As soon as bar is imported, I get this error message :

NameError: name 'foo' is not defined

解决方案

If you want to refer to a name in another module then you must import it.

import foo

class bar(foo.foo):
   ...

这篇关于类继承从不同的模块python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆