Home > other >  Just learning Python, or white, and then looking at simplify data structure, have some questions abo
Just learning Python, or white, and then looking at simplify data structure, have some questions abo

Time:11-03



(1) class Structure:
_fields=[]

Def __init__ (self, * args, * * kwargs) :
If len (args) & gt; Len (self. _fields) :
Raise TypeError (' Expected {} the arguments'. The format (len (self. _fields)))
For the name, the value in the zip (self _fields, args) :
Setattr (self, name, value)
For the name in the self. _fields [len (args) :] :
Setattr (self, name, kwargs. Pop (name))
,,,,,,,,
(2) extra_args=kwargs. Keys () - self. _fields
For the name in extra_args:
Setattr (self, name, kwargs. Pop (name))
,,,,,,,,
(3) the class Stock (Structure) :
_fields=[' name ', 'shares',' prices']

S=Stock (ACER, 50, prices=99, the date='2012-2-2')
Print (s.n ame, s.s hares, supachai panitchpakdi rices, spyware doctor ate)


Why does it feel (1) the if len (args) & gt; Len (self _fields) refers to the actual key can't more than the field, then don't and (2) the code inside contradiction?
(3) _fields=[' name ', 'shares',' prices'], only three, but the actual s inside still have one more date dimension, I am not a computer professional, I was just learning, if ask more silly also please forgive me

CodePudding user response:

Kwargs, args is location parameter, is the key word parameters, this want to distinguish
S=Stock (ACER, 50, prices=99, the date='2012-2-2')
This sentence execution, 'ACER and 50 are location parameter, to the args, while prices=99 and date=' 2012-2-2 'to kwargs, the rest is well understood
  • Related