python - How to modify an inherited list in the __init__ method of a subclass? -


is possible change in init function of class?

lets have class called "deck" which, when initialized, creates list of 52 card objects.

now want make class called "even" inherits "deck" class , creates deck of card objects eliminates cards number 2 (so spades, hearts, etc) inherited "deck".

i have been having lot of trouble because when try modify inherited list, regardless of try, python return error, 'nonetype' being main root of problem. here code "even" class init:

def __init__(self):     x = deck.__init__(self)     card in x:         if card.rank() == 2:             x.pop(card)     return x 

it worth noting card class has method rank() return rank of card int.

regardless of things have tried, there wrong it. "'nonetype' object not iterable" or "subscripable" , when check type() of x nonetype. have done lot of searching around nothing making sense me nonetype or should fix it.

if remove loop code create deck of 52 cards expected, need filter out 2's example.

edit: deck class init:

class deck(list):     def __init__(self):         return list.__init__(self, [card(i) in range(52)]) 

if cant tell, card class , init of deck creates 52 card objects

so assuming simple version of deck baseclass, inheriting modifying list of cards stored in baseclass done like:

# simple baseclass contains list of card objects class deck(object):     def __init__(self):         self.deck = [...] # list of cards  # class hold cards class even(deck)     def __init__(self):         # use super instantiate baseclass         super(even,self).__init__(self)          # create local instance of `deck` contains cards         # base set of cards can still acessed via super(even,self).deck         self.deck = [card card in self.deck if card.rank() != 2] 

edit: updating added information in question:

class deck(list):     def __init__(self):         super(deck,self).__init__([card(i) in range(52)])  class even(deck)     def __init__(self):         super(even,self).__init__(self)         [self.remove(card) card in self if card.rank()==2] 

Comments

Popular posts from this blog

c++ - How to add Crypto++ library to Qt project -

jQuery Mobile app not scrolling in Firefox -

How to use vim as editor in Matlab GUI -