无法导入python类

时间:2018-11-19 17:06:56

标签: python python-3.x

我在导入时有一个简单的错误 但是如何解决呢?;)) 我是菜鸟,正在测试任务

其中一个条件是将父类与子类分开! 也就是说,一切都因其位置[classes_1.py,classes_2.py]

而好

主要:

n10_12.py

from classes_1 import User
from classes_2 import Privileges, Admin

admin_33 = Admin('mike', 'logsdale', 'africa', 'customer')

admin_33.privilege.show_privileges()
admin_33.descibe_user()

分类两个文件:

1)

classes_1.py

class User():

    def __init__(self, first_name, last_name, location='', field=''):
        self.first_name = first_name
        self.last_name = last_name
        self.location = location
        self.field = field
        self.full_name = self.first_name.title() + ' ' + self.last_name.title()
        self.login_attemts = 0

    def descibe_user(self):

        print('\nFull name: ' + self.full_name + 
            '\nLocation: ' + self.location.title() + 
            '\nField: ' + self.field.title() + '\n'
            )

    def greet_user(self):
        print('Hello, ' + self.full_name)

    def read_login_attemts(self):
        print('In this time login attempts are ' + str(self.login_attemts))

    def increment_login_attempts(self):
        self.login_attemts += 1

    def reset_login_attempts(self):
        self.login_attemts = 0

2)

classes_2.py

class Privileges():
    def __init__(self):
        self.privileges = [
                    'access to add msg',
                    'access to delete users',
                    'access to ban users',
                     ]


    def show_privileges(self):
        print('Hi Admin, your privileges is: ')
        for kk in self.privileges:
            print('\n- ' + kk.lower())




class Admin(User):

    def __init__(self, first_name, last_name, location='', field=''):
        super().__init__(first_name, last_name, location, field)
        self.privilege = Privileges()

跟踪:

python n10_12.py

回溯(最近通话最近一次):

文件“ n10_12.py”,第2行,在     从classes_2导入权限,管理员

文件.. \ classes_2.py”,第28行,在     类别Admin(User):

NameError:未定义名称“用户”

1 个答案:

答案 0 :(得分:4)

您需要在class_2.py中导入User:

malloc