您的位置:首页 > 理论基础 > 数据结构算法

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

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

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 99
4 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 99
4 5


还可以利用关键字参数来给类添加额外的属性,而这些额外的属性是没有定义在_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))
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)


从示例中可
4000
以发现:我们都是使用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,但是不仅会增加程序员的工作量,而且运行速度也比较慢,所以不推荐。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息