简体   繁体   English

Python:在 Class ZE40489CD1E7102E35469C937E05C8

[英]Python: Passing Optional Arguments in Class Inheritance

I don't understand why I cannot set my optional arguments to values.我不明白为什么我不能将可选的 arguments 设置为值。 I always receive an empty list.我总是收到一个空列表。 I have the argument set as Nonetype to avoid having a mutable type there instead.我将参数设置为 Nonetype 以避免在那里使用可变类型。 Is it not updating due to an oversight in my parent classes or am is it something else?是不是因为我的父类疏忽而没有更新,还是其他原因?

class Human:
  def __init__(self,first,last,age,nicknames=None):
    self.first =first
    self.last = last
    self.age = age

    if nicknames is None:
      self.nicknames = []
    else:
      self.nicknames = nicknames


class NFL_QB:
  def __init__(self,td,comeback,lst=None):
    self.td = td
    self.comeback = comeback

    if lst is None:
      self.lst = []
    else:
      self.lst = lst


class College_QB:
  def __init__(self,c_td,c_comeback):
    self.c_td = c_td
    self.c_comeback = c_comeback


class Player(Human,NFL_QB,College_QB):
  def __init__(self,first,last,age,td,comebacks,c_td,c_comebacks,nicknames=None,lst=None):
    Human.__init__(self,first,last,age,nicknames=None)
    NFL_QB.__init__(self,td,comebacks,lst=None)
    College_QB.__init__(self,c_td,c_comebacks)




ply = Player('tom','brady',42,532,55,32,21,['toutchdown tom'],[2016])

Pass the args down to your superclass constructors:将参数传递给您的超类构造函数:

class Player(Human,NFL_QB,College_QB):
  def __init__(self,first,last,age,td,comebacks,c_td,c_comebacks,nicknames=None,lst=None):
    Human.__init__(self,first,last,age,nicknames=nicknames) # You were passing `None` here
    NFL_QB.__init__(self,td,comebacks,lst=lst)  # ...and here.
    College_QB.__init__(self,c_td,c_comebacks)




ply = Player('tom','brady',42,532,55,32,21,['toutchdown tom'],[2016])
print(ply.__dict__)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM