用于返回字典的Python类

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (99)

我在网上研究过,但仍然很难理解如何创建课程。SoloLearn,DataCamp和其他站点中的示例通常使用Person/ Employeeexample,例如:

class Person:

    def __init__(self, first, last):
        self.firstname = first
        self.lastname = last

    def Name(self):
        return self.firstname + " " + self.lastname

class Employee(Person):

    def __init__(self, first, last, staffnum):
        Person.__init__(self,first, last)
        self.staffnumber = staffnum

    def GetEmployee(self):
        return self.Name() + ", " +  self.staffnumber

x = Person("Marge", "Simpson")
y = Employee("Homer", "Simpson", "1007")

print(x.Name())
print(y.GetEmployee())

我正在尝试创建一个示例类,通过稍微修改它来返回字典。这是代码:

 class Place:

    def __init__(self, country, province):
        self.country= country
        self.province = province

    def CountryState(self):
        return {
            'Province': self.province,
            'Country': self.country
            }

class Location(Place):

    def __init__(self, country, province, city, street, postal):
        super(Place, self).__init__(country, province, city, street, postal)
        self.city = city
        self.street = street
        self.postal = postal

    def GetLocation(self):
        return CountryState().update({
            'City': self.city,
            'Street': self.street,
            'Postal': self.postal
            })

if __name__=="__main__":

    x = Place('United States', "California")
    y = Location("United States", "California", "Los Angeles", "Johnson Street", "90007")

    print(x.CountryState())
    print(y.GetLocation())

我使用以下命令获取跟踪pdb.set_trace()

Traceback (most recent call last):
  File "/home/john/Python/class-example.py", line 37, in <module>
    y = Location("United States", "California", "Los Angeles", "Johnson Street", "90007")
  File "/home/john/Python/class-example.py", line 22, in __init__
    super(Place, self).__init__(country, province, city, street, postal)
TypeError: object.__init__() takes no parameters
> /usr/lib/python3.5/idlelib/run.py(370)runcode()
-> jit = self.rpchandler.console.getvar("<<toggle-jit-stack-viewer>>")

pdb list

365                 self.usr_exc_info = sys.exc_info()
366                 if quitting:
367                     exit()
368                 # even print a user code SystemExit exception, continue
369                 print_exception()
370  ->             jit = self.rpchandler.console.getvar("<<toggle-jit-stack-viewer>>")
371                 if jit:
372                     self.rpchandler.interp.open_remote_stack_viewer()
373             else:
374                 flush_stdout()
375     

我无法__init__接受参数。请让我知道我错过了什么,所以我可以从这些行返回字典:

print(x.CountryState())
print(y.GetLocation())

{ 'Country': 'United States', 'State': 'California' }

{ 'Country': 'United States', 'City': 'Los Angeles', 'Street':'Johnson Street', 'Postal Code': '90007'}
提问于
用户回答回答于

Location(Place)你打电话的课堂上

super(Place, self).__init__(country, province, city, street, postal)

你应该调用类的超Location类。现在,你正在打电话给超级班Place。由于Place没有声明的超类,它派生自object类(它是所有类的父类),并且您正在调用object构造函数,它不带任何参数。

此外,即使您修复了这个问题,您提供的参数也多于Place所需的参数。正确的形式是

super(Location, self).__init__(country, province)

扫码关注云+社区

领取腾讯云代金券

年度创作总结 领取年终奖励