前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python学习笔记6.7-简化数据结构的初始化过程

python学习笔记6.7-简化数据结构的初始化过程

作者头像
锦小年
发布2018-01-02 14:27:13
6490
发布2018-01-02 14:27:13
举报
文章被收录于专栏:锦小年的博客

我们每编写一个类的时候都需要编写一个初始化函数,那么如果编写的类当做数据结构来用,它们的初始化结构就是一样的,例如:

代码语言:javascript
复制
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()函数中,并将其定义在公共的基类中:

代码语言:javascript
复制
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 99
4 5

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

代码语言:javascript
复制
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 99
4 5

还可以利用关键字参数来给类添加额外的属性,而这些额外的属性是没有定义在_fields中的:

代码语言:javascript
复制
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))
        extra_args = kwargs.keys() - self._fields
        for name in extra_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,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()函数来将传递进来的属性参数添加到对应的属性上。与此不同的是,还可以选择直接访问示例字典的办法来添加属性。

代码语言:javascript
复制
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的文档和帮助,如果用户针对于某个特定的类寻求帮助,那么所需的参数就不会以正常的形式来表达。

代码语言:javascript
复制
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,但是不仅会增加程序员的工作量,而且运行速度也比较慢,所以不推荐。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档