0

Can anyone tell me how to bind to specific child class at run time in the following code? I want mCar instance in the following example redirect to class Truck or Compact according to command line options?

class Car(object):
    pass

class Truck(Car):
    pass

class Compact(Car):
    pass

and a instance of Car

mCar = Car()
gahooa
  • 131,293
  • 12
  • 98
  • 101
WuJanJai
  • 3
  • 1
  • This code works. What's your question? – S.Lott Sep 16 '09 at 19:46
  • As suggested by GHZ, I want to bind mCar to Truck or Compact at run time according to certain run time condition. – WuJanJai Sep 16 '09 at 19:50
  • "certain run-time condition"? What does that mean? Do you mean an if-statement? Can you provide some example code of what you are trying to do? – S.Lott Sep 16 '09 at 19:52
  • I think this thread http://stackoverflow.com/questions/456672/class-factory-in-python suits my need. Thanks – WuJanJai Sep 16 '09 at 19:57

2 Answers2

4

You mean like this?

car_classes = {
'car'     : Car,
'truck'   : Truck,
'compact' : Compact
}

if __name__ == '__main__':
    option = sys.argv[1]
    mCar = car_classes[option]()
    print 'I am a', mCar.__class__.__name__
GHZ
  • 3,365
  • 4
  • 24
  • 28
  • Hi, Gahooa I think it should work in some cases. I am curious if there other ways like "typecasting" or change "__class__" in Car::__init__() Thanks – WuJanJai Sep 16 '09 at 19:31
  • 1
    Well you could use a class factory or even __metaclass__ http://en.wikibooks.org/wiki/Python_Programming/MetaClasses but unless you really need it, it's likely to be overkill. – GHZ Sep 16 '09 at 19:42
1

As a side note, while not particularly recommended, it IS possible to assign a different value to self.__class__ -- be that in __init__ or anywhere else. Do notice that this will change the lookups for class-level names (such as methods), but per se it will not alter the instance's state (nor implcitly invoke any kind of initialization -- you'll have to do it explicitly if you need that to happen)... these subtleties are part of why such tricks are not particularly recommended (along with the general cultural bias of Pythonistas against "black magic";-) and a "factory function" (which in especially simple cases can be reduce to a dict lookup, as in GHZ's answer) is the recommended approach.

Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395
  • Thanks a lot for the explanation. The assignment of self.__class__ is what I saw in a third party code. But it looks to me that it needs to be delt with very carefully, as what you mentioned. – WuJanJai Sep 17 '09 at 17:45