为什么此代码无法从导入的模块成功运行,即使当整个代码从同一页面运行时它运行得很好

问题描述 投票:0回答:2

我正在尝试通过从本地目录导入来测试我的模块。当创建实例并从同一页面进行函数调用(无需导入)时,此代码可以完美运行。但是,当我将代码块移动到模块并导入它时,它不断返回错误,如下所示:

以下代码来自我本地存储的名为 Admin.py 的模块:

class User:
    def __init__(self, name, last, age, nationality):
        self.name = name
        self.last = last
        self.age = age
        self.nationality = nationality
        
    def describe_user(self):
        print(f"\nAdmin name is {self.name.title()}, last name is {self.last.title()}, age is {self.age} and is of {self.nationality.title()} nationality\n")
        
    def greet_user(self):
        print(f"\nHello! {self.name.title()}, welcome to the XYZ Consultancy Services Ltd.\n")
        
class Privileges:
    def __init__(self, privileges):
        self.privileges = privileges

    def show_privileges(self):
            print("Privileges of the admin are:\n")
            for privilege in self.privileges:
                print(f"- {privilege}")

class Admin(User):
    def __init__(self, name, last, age, nationality):
        super().__init__(name, last, age, nationality)
        self.privileges = Privileges(privileges)

privileges = ["can add post", "can delete post", "can ban user"]

以下代码来自我尝试从本地模块 Admin.py 导入代码块并尝试执行它的页面。

from user import Admin


priv = Admin("veronica", "smith", 26, 'American')

priv.greet_user()

priv.describe_user()

priv.privileges.show_privileges()

以下是返回的输出(错误):

   92 #         super().__init__(name, last, age, nationality)
     93 #         self.privileges = Privileges(privileges)
     95 privileges = ["can add post", "can delete post", "can ban user"]
---> 97 priv = Admin("veronica", "smith", 26, 'American')
     99 priv.describe_user()
    101 priv.privileges.show_privileges()

   25 def __init__(self, name, last, age, nationality):
     26     super().__init__(name, last, age, nationality)
---> 27     self.privileges = Privileges()

TypeError: Privileges.__init__() missing 1 required positional argument: 'privileges'

当整个代码从同一页面运行时,我得到了我所期望的以下内容:

Hello! Veronica, welcome to the XYZ Consultancy Services Ltd.


Admin name is Veronica, last name is Smith, age is 26 and is of American nationality

Privileges of the admin are:

- can add post

- can delete post

- can ban user

我已经尝试了几乎所有可能的排列/组合来修复这个位置参数错误,但即使经过 6 个小时的工作我也无法解决它。

python-3.x
2个回答
0
投票

如果按原样运行代码,则列表

priviledges
在运行时是已知的。如果您从用作模块的代码导入,则类
Admin
没有有关此列表的信息。下面修改后的代码应该按预期运行:

class User:
    def __init__(self, name, last, age, nationality):
        self.name = name
        self.last = last
        self.age = age
        self.nationality = nationality
        
    def describe_user(self):
        print(f"\nAdmin name is {self.name.title()}, last name is {self.last.title()}, age is {self.age} and is of {self.nationality.title()} nationality\n")
        
    def greet_user(self):
        print(f"\nHello! {self.name.title()}, welcome to the XYZ Consultancy Services Ltd.\n")
        
class Privileges:
    def __init__(self, privileges):
        self.privileges = privileges

    def show_privileges(self):
            print("Privileges of the admin are:\n")
            for privilege in self.privileges:
                print(f"- {privilege}")

class Admin(User):
    def __init__(self, name, last, age, nationality):
        super().__init__(name, last, age, nationality)
        self.privileges = Privileges(["can add post", "can delete post", "can ban user"])

0
投票

您的问题在这里:

privileges = ["can add post", "can delete post", "can ban user"]

您可以在文件中定义此列表,但不在任何类中定义。此类连接模块不提供。如果我是你,我可能会将这个选项列表包含在

Privileges
中。

这样的东西应该可以正常工作:

class Privileges:
def __init__(self, privileges = []):
    self.privileges = privileges

def show_privileges(self):
        print("Privileges of the admin are:\n")
        for privilege in self.privileges:
            print(f"- {privilege}")

您还应该以某种方式将权限选项包含在

greet_user()
中。

如果此解决方案不适合您的偏好,至少将权限选项移至某个类中以使其成为类字段。如果是这样,您可以访问它并用作模块功能。 (第一次回答,如有错误请指正。)

© www.soinside.com 2019 - 2024. All rights reserved.