首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >用于返回字典的Python类

用于返回字典的Python类
EN

Stack Overflow用户
提问于 2018-08-19 01:03:10
回答 1查看 1K关注 0票数 0

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

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'}
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51910868

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档