Inheritance question
Alex Martelli
aleaxit at yahoo.com
Mon Sep 20 10:39:19 EDT 2004
Yannick Turgeon <nobody at nowhere.com> wrote:
> class A:
> _value
> __init__(self, value):
> self._value = value
> if value > 10:
> set myself as an instance of B1 # How can I do that?
self.__class__ = B1
Whether you WANT to do that is quite another issue: you most likely
don't, though you wrongly believe you do. Use a factory-function
pattern if you want to generate different possible classes, don't use
black magic such as changing an instance's class on the fly unless there
are _really good_ reasons (an example of a really good reason, IMHO, is
given by a recipe in the Cookbook that presents a bounded-ring-buffer
class... its instances commute from NotFull to Full classes once and for
all when they switch from non-full to full, NOT at instance generation
time...).
Alex
More information about the Python-list
mailing list