How to pass parameters to parent class with type() in python? -


class parent(object):      def __init__(self, first):         self.first = first  class child1(parent):      def __init__(self):         parent.__init__(self, 'a') 
  • i have parent class , want make 12 similar classes inherit parent class, same signature , no parameters. how define child1, using type()? far have child1 = type('child1', (rank,), dict()) how can pass first parent class?

you use attributes dictionary. think should trick.

def genericinit(self):     parent.__init__(self, 'a')  type('child1', (rank,), {'__init__': genericinit}) 

updated per comments: use functools.partial

attrdict = {'init': functools.partial(parent.__init__, first='a')} 

but bad code. don't want calling init anywhere other init because it's confusing using class.


Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -