English 中文(简体)
为什么这一法典未能成功地从进口模块中操作,尽管它运行得很好,但从同一页上操作。
原标题:Why this code fails to run successfully from imported module even though it runs very well when this entire code is run from the same page
  • 时间:2024-04-20 14:27:37
  •  标签:
  • python-3.x

我试图通过从我的当地目录进口我的模块来测试我的模块。 该法典在设立案件时毫无用处,职能要求从同一页(无进口)上发出。 然而,当我把代码块移至模块并进口时,它保留了以下回归错误:

Following code is from my locally stored module named 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"
Admin name is {self.name.title()}, last name is {self.last.title()}, age is {self.age} and is of {self.nationality.title()} nationality
")
        
    def greet_user(self):
        print(f"
Hello! {self.name.title()}, welcome to the XYZ Consultancy Services Ltd.
")
        
class Privileges:
    def __init__(self, privileges):
        self.privileges = privileges

    def show_privileges(self):
            print("Privileges of the admin are:
")
            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()

Following is the returned output (error):

   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 

When this entire code is run from the same page, I get the following which is expected by me:

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小时之后,我也未能解决这一错误。

问题回答

Outcomes

After test You Snippet in Conditions

  1. Same File

    • Works Perfectly
  • 完成 如果有,

    • Here, filename is user.py
    from user import Admin
    
    • If trying from Admin.py
    from Admin import Admin
    

www.un.org/spanish/ecosoc 其他工作方式!

  1. Using the globals() to list the global variables
class Admin(User):
    def __init__(self, name, last, age, nationality):
        super().__init__(name, last, age, nationality)
        self.privileges = Privileges(globals()[ privileges ])

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

这份清单将列出<代码>globals(>,并注明使用。

I hope this answer help you in any way,Thank you..

如果按现在的顺序排列,则名单<代码>privileges为时常识。 如果您从作为模块使用的代码中进口Admin,则该类别<代码>Admin没有该清单的资料。 略作修改的代码,列出需要的清单,即按预期修改的类别代码:<代码>Admin。

user.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"
Admin name is {self.name.title()}, last name is {self.last.title()}, age is {self.age} and is of {self.nationality.title()} nationality
")
        
    def greet_user(self):
        print(f"
Hello! {self.name.title()}, welcome to the XYZ Consultancy Services Ltd.
")
        
class Privileges:
    def __init__(self, privileges):
        self.privileges = privileges

    def show_privileges(self):
            print("Privileges of the admin are:
")
            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"])

www.un.org/french/ga

from user import Admin


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

priv.greet_user()

priv.describe_user()

priv.privileges.show_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

如果你想保持项目目标设计,我建议把特权清单作为特设类别参数如下:

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"
Admin name is {self.name.title()}, last name is {self.last.title()}, age is {self.age} and is of {self.nationality.title()} nationality
")
        
    def greet_user(self):
        print(f"
Hello! {self.name.title()}, welcome to the XYZ Consultancy Services Ltd.
")
        
class Privileges:
    def __init__(self, privileges):
        self.privileges = privileges

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

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

你的问题是:

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

你在档案中界定了这一清单,但不是在任何类别之内。 这些连接模块没有提供。 如果我是的话,我也许会把这个选项清单列入<条码>。

这一点值得一提:

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

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

并且,请在<条码>范围内有一些特权选择。

If this solution not fit for your preference at least move privileges options inside some class to make it a class field. If so you could access it and use as a module functionality. (It s my first answer, please correct me if I somewhere was wrong.)

你们提供的守则就是一例。 造成“类型错误”的代码与代码之间的区别很可能是没有提供<代码>privileges到“>.privileges = 特权(privileges)。 可从错误信息中看出,这一呼吁是不同的<条码>。

您在问询中将文件称为Admin,然而,在你使用用户的榜样中。

我已经找到了理由。 特权必须增加,作为“init(自称, foo, foo, foo, foo, 特权)方法的属性。 我从单元档案中删除了特权清单,并将其移至我的工作档案中。 现在,我可以创造和利用更新职能,在我不想修改单元档案的情况下更新我的特权清单。 模块档案不再重新命名。





相关问题
Get webpage contents with Python?

I m using Python 3.1, if that helps. Anyways, I m trying to get the contents of this webpage. I Googled for a little bit and tried different things, but they didn t work. I m guessing that this ...

What is internal representation of string in Python 3.x

In Python 3.x, a string consists of items of Unicode ordinal. (See the quotation from the language reference below.) What is the internal representation of Unicode string? Is it UTF-16? The items ...

What does Python s builtin __build_class__ do?

In Python 3.1, there is a new builtin function I don t know in the builtins module: __build_class__(...) __build_class__(func, name, *bases, metaclass=None, **kwds) -> class Internal ...

what functional tools remain in Python 3k?

I have have read several entries regarding dropping several functional functions from future python, including map and reduce. What is the official policy regarding functional extensions? is lambda ...

Building executables for Python 3 and PyQt

I built a rather simple application in Python 3.1 using PyQt4. Being done, I want the application to be distributed to computers without either of those installed. I almost exclusively care about ...

热门标签