Python语言入门6.7-简化数据结构的初始化过程
小标 2018-06-20 来源 : 阅读 767 评论 0

摘要:在Python语言入门中,我们每编写一个类的时候都需要编写一个初始化函数,那么如果编写的类当做数据结构来用,它们的初始化结构就是一样的,希望对大家学习Python语言入门有所帮助。

在Python语言入门中,我们每编写一个类的时候都需要编写一个初始化函数,那么如果编写的类当做数据结构来用,它们的初始化结构就是一样的,希望对大家学习Python语言入门有所帮助。例如:

class Stock:
    def __init__(self,name,shares,price):
        self.name = name
        self.shares = shares
        self.price = price
class Point:
    def __init__(self,x,y):
        self.x = x
        self.y = y
class Circle:
    def __init__(self,radius):
        self.radius = radius

每一个类写一个数据结构这样比较麻烦,可以利用其它有效的方法去避免这种麻烦,加快编写代码的速度,例如,将初始化数据结构的步骤归纳到一个单独的init()函数中,并将其定义在公共的基类中:

class Structure:
    _fields=[]
    def __init__(self,*args):
        if len(args) != len(self._fields):
            raise TypeError('Expected {} arguements'.format(len(self._fields)))
        #set the arguments
        for name, value in zip(self._fields,args):
            setattr(self,name,value)
class Stock(Structure):
    _fields = ['name','shares','prices']
s = Stock('ACER',50,99)
print(s.name,s.shares,s.prices)
class Point(Structure):
    _fields = ['x','y']
p = Point(4,5)
print(p.x,p.y)
 
打印输出:
ACER 50 994 5

这样我们还可以继续创建各种基于Structure的类,方便快捷。究其本质而言,这种方法很好的利用了类的继承。 
可以对上面的方法进行完善,对其添加对关键字参数的支持,这样表达更清晰,更方便编程,最好的选择就是对关键字参数做映射,这样它们就只能对应于定义在-fields中的属性名:

class Structure:
    _fields=[]
    def __init__(self,*args,**kwargs):
        if len(args) > len(self._fields):
            raise TypeError('Expected {} arguments'.format(len(self._fields)))
        #set the arguments
        for name, value in zip(self._fields,args):
            setattr(self,name,value)
        for name in self._fields[len(args):]:
            setattr(self,name,kwargs.pop(name))
        if kwargs:
            raise TypeError('Invided arguments : {}'.format(','.join(kwargs)))
class Stock(Structure):
    _fields = ['name','shares','prices']
s = Stock('ACER',50,prices=99)
print(s.name,s.shares,s.prices)
class Point(Structure):
    _fields = ['x','y']
p = Point(x=4,y=5)
print(p.x,p.y)

 

打印输出:

ACER 50 994 5
class Stock(Structure): _fields = ['name','shares','prices'] s = Stock('ACER',50,prices = 99,date = '2012-2-2') print(s.name,s.shares,s.prices,s.date) class Point(Structure): _fields = ['x','y'] p = Point(4,5) print(p.x,p.y)

从示例中可以发现:我们都是使用setattr()函数来将传递进来的属性参数添加到对应的属性上。与此不同的是,还可以选择直接访问示例字典的办法来添加属性。

class Structure:
    _fields=[]
    def __init__(self,*args,**kwargs):
        if len(args) > len(self._fields):
            raise TypeError('Expected {} arguments'.format(len(self._fields)))
        #set the arguments
        self.__dict__.update(zip(self._fields,args))
class Stock(Structure):
    _fields = ['name','shares','prices']
s = Stock('ACER',50,99)
print(s.name,s.shares,s.prices)
class Point(Structure):
    _fields = ['x','y']
p = Point(4,5)
print(p.x,p.y)

这么写看似高端简单,但是对于python程序来说是不严谨的。如果某个子类使用slot()方法或者@property(或者描述符)包装了某个特定的属性,直接访实例的字典就会产生崩溃。因而不建议使用这种写法。

尽管简化数据结构的几种方法都十分的实用,但是它的缺点就是会影响到IDE的文档和帮助,如果用户针对于某个特定的类寻求帮助,那么所需的参数就不会以正常的形式来表达。

print(help(Stock))
打印输出:class Stock(Structure)
 |  Method resolution order:
 |      Stock
 |      Structure
 |      builtins.object
 |  
 |  Data and other attributes defined here:
 |  
 |  _fields = ['name', 'shares', 'prices']
 |  
 |  ----------------------------------------------------------------------
 |  Methods inherited from Structure:
 |  
 |  __init__(self, *args, **kwargs)
 |      Initialize self.  See help(type(self)) for accurate signature.
 |  
 |  ----------------------------------------------------------------------
 |  Data descriptors inherited from Structure:
 |  
 |  __dict__
 |      dictionary for instance variables (if defined)
 |  
 |  __weakref__
 |      list of weak references to the object (if defined)
 
None

虽然可以通过在init()中强制签名来解决这个bug,但是不仅会增加程序员的工作量,而且运行速度也比较慢,所以不推荐。

以上就介绍了Python的相关知识,希望对Python有兴趣的朋友有所帮助。了解更多内容,请关注职坐标编程语言Python频道!

本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 1 不喜欢 | 0
看完这篇文章有何感觉?已经有1人表态,100%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程