return an object of a different class

spam at spam at
Wed Feb 16 03:23:39 CET 2011

How can I do something like this in python:


class MyNumbers:
   def __init__(self, n):
     self.original_value = n
     if n <= 100:
       self = SmallNumers(self)
       self = BigNumbers(self)

class SmallNumbers:
   def __init__(self, n):
     self.size = 'small'

class BigNumbers:
   def __init__(self, n):
     self.size = 'big'

t = MyNumbers(200)

When I do type(t) it says MyNumbers, while I'd want it to be BigNumbers, 
because BigNumbers and SmallNumbers will have different methods etc...

Do I need to use metaclasses?


More information about the Python-list mailing list