Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么我给出正确的位置参数,但仍然返回错误消息?_Python - Fatal编程技术网

Python 为什么我给出正确的位置参数,但仍然返回错误消息?

Python 为什么我给出正确的位置参数,但仍然返回错误消息?,python,Python,我对以下代码有问题: class User: def __init__(self, first_name, last_name, **profile): self.profile = profile self.first_name = first_name self.last_name = last_name` self.profile['first_name'] = first_name self.profile['last_name'] = last

我对以下代码有问题:

class User:

def __init__(self, first_name, last_name, **profile):
    self.profile = profile
    self.first_name = first_name
    self.last_name = last_name`
    self.profile['first_name'] = first_name
    self.profile['last_name'] = last_name

def query(self, key):
    value = self.profile[key]
    print('Here is the result:' + '\n\t' + str(value))


class UserVip(User):
def __init__(self, first_name, last_name, **profile):
    super().__init__(self, first_name, last_name, **profile)


ron_weasley = UserVip('ron', 'weasley', 
                  age=17,`enter code here
                  school='hogwarts'
                  )
当我运行它时,我得到了这个错误消息

TypeError: __init__() takes 3 positional arguments but 4 were given
但是“**配置文件”可以接收任意数量的参数,不是吗?然后我检查了参数,并像这样尝试

ron_weasley = UserVip('ron', 'weasley', 
                      school='hogwarts'
                      )

我还试过其他方法,在网上搜索了很长时间。但是没有用。我是否误解了init()、inherit或“**(dict)”?请帮助或尝试给出一些解决方法。提前感谢。

首先,UserVip类的
\uuuu init\uuuu
的缩进是错误的

其次,对于UserVip的超类构造函数,应该使用
User
而不是
super()
。或者你可以把self从论点中去掉

下面的代码适合我

class User:

    def __init__(self, first_name, last_name, **profile):
        self.profile = profile
        self.first_name = first_name
        self.last_name = last_name
        self.profile['first_name'] = first_name
        self.profile['last_name'] = last_name

    def query(self, key):
        value = self.profile[key]
        print('Here is the result:' + '\n\t' + str(value))


class UserVip(User):
    def __init__(self, first_name, last_name, **profile):
        User.__init__(self, first_name, last_name, **profile)


ron_weasley = UserVip('ron', 'weasley', age=17,school='hogwarts')

除了
super
,您还可以在代码中使用
classname

例如,使用下面的代码调用父类
\uuuuu init\uuuu

User.\uuuuu init\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu


希望它能解决你的问题

在调用parent
\uuuuu init\uuuu

Ie应该是:
super()。
下面是一个正在运行的代码:

class User:
    def __init__(self, first_name, last_name, **profile):
        self.profile = profile
        self.first_name = first_name
        self.last_name = last_name
        self.profile['first_name'] = first_name
        self.profile['last_name'] = last_name

    def query(self, key):
        value = self.profile[key]
        print('Here is the result:' + '\n\t' + str(value))


class UserVip(User):
    def __init__(self, first_name, last_name, **profile):
        super().__init__(first_name, last_name, **profile)


ron_weasley = UserVip('ron', 'weasley', age=17, school='hogwarts')
ron_weasley.query('age')

使用classname,您只需调用该类的方法并传递包括self在内的所有参数。因此,当它得到所需的所有论据时,它就起作用了。