简体   繁体   English

Python子类,其属性少于父级

[英]Python child class with fewer attributes than parent

I'm a bit confused here. 我在这里有点困惑。 In two different scripts I've created a child class that has fewer attributes than its parent class. 在两个不同的脚本中,我创建了一个子类,它的属性少于其父类。 For some reason in one case the code works without issues and in the other I constantly get TypeErrors for missing positional arguments. 出于某种原因,在一种情况下,代码可以正常工作,而在另一种情况下,我经常会因缺少位置参数而获得TypeErrors。

This code works without any issues: 此代码没有任何问题:

class Restaurant:
    def __init__(self, restaurant_name, cuisine_type):
        self.name = restaurant_name
        self.cuisine = cuisine_type
        self.number_served = 0

--SNIP--

class IceCreamStand(Restaurant):
    def __init__(self, restaurant_name):
        super().__init__(self,restaurant_name)
        self.name = restaurant_name

but this, which looks identical to me, constantly fails for some in the line super().__init__(self, first_name) saying that init is missing 2 additional positional arguments. 但是这看起来和我相同,对于行中的一些super().__init__(self, first_name)一直失败,说init缺少2个额外的位置参数。

class User:
    def __init__(self, first_name, last_name, location, occupation):
        self.name = first_name
        self.surname = last_name
        self.location = location
        self.job = occupation
        self.login_attempts = 0

--SNIP--

class Privileges(User):
    def __init__(self, first_name):
        super().__init__(self, first_name)
        self.name = first_name
        self.privileges_admin = ('can add posts'
                           , "can edit other users' posts", 'can delete posts',
                           'can modify posts', 'can ban users')
        self.privileges_user = ('can add posts', 'can edit own posts')

Can anyone please explain why this happens? 任何人都可以解释为什么会这样吗?

When you use super().func() 当你使用super()。func()时

In my opinion, it mean you call the parent method. 在我看来,这意味着你调用父方法。

Mean you should pass the same args in parent method. 意思是你应该在父方法中传递相同的args。

class User:
    def __init__(self, first_name, last_name, location, occupation):

Like this: 像这样:

class Privileges(User):
    def __init__(self, first_name):
        super().__init__(first_name, 'default', 'default', 'default')

Or: 要么:

class Privileges(User):
    def __init__(self, first_name, last_name, location, occupation):
        super().__init__(first_name, last_name, location, occupation)

I am not very good at english. 我不是很擅长英语。 Hope it will help 希望它会有所帮助

You use super().__init__() in wrong way - you have to run it without self . 你以错误的方式使用super().__init__() - 你必须在没有self情况下运行它。

Restaurant.__init__() expects two values restaurant_name, cuisine_type (not three) and in IceCreamStand.__init__() using super().__init__() you send to Restaurant.__init__() two values self and restaurant_name - but they are assigned in different way that you expect Restaurant.__init__()需要两个值restaurant_name, cuisine_type (不是三个)和IceCreamStand.__init__()使用super().__init__()你发送到Restaurant.__init__()两个值selfrestaurant_name - 但它们被分配在不同的你期望的方式

restaurant = self 
cuisine_type = restaurant_name

If in Restaurant.__init__() you add 如果在Restaurant.__init__()添加

print(restaurant_name, cuisine_type)

then you will see your mistake. 然后你会看到你的错误。


User.__init__() expects 4 values, not 5 - first_name, last_name, location, occupation . User.__init__()需要4个值,而不是5 - first_name, last_name, location, occupation In Privileges.__init__() using super().__init__() you send 2 values to User.__init__() but it needs 2 values more and you get error message "missing 2 additional positional arguments" . Privileges.__init__()使用super().__init__()您将2个值发送给User.__init__()但它需要2个值,并且您会收到错误消息"missing 2 additional positional arguments"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM