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

```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:

```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```

```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```

```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)```

```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)```

```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```

60 篇文章34 人订阅

0 条评论

## 相关文章

1573

1005

3497

2126

3074

2867

### BZOJ 1293: [SCOI2009]生日礼物【单调队列】

1293: [SCOI2009]生日礼物 Time Limit: 10 Sec  Memory Limit: 162 MB Submit: 2534  Solv...

2565

2124

972

37012