return an object of a different class
spam at uce.gov
spam at uce.gov
Wed Feb 16 00:06:22 EST 2011
On 11-02-15 07:45 PM, alex23 wrote:
>
> Firstly, does MyNumbers _have_ to be a class? Or would a function
> acting as a class factory be sufficient?
Yes it does. I didn't explain my problem, chose a terrible example. This is
more what I'm trying to do:
class thingy:
def __init__(self, athingy):
self.basic_extract()
if self.sortof =
def basic_extract(self):
do a bunch of things
self.sortof = ..
def general_method(self)
class ThingyTypeA:
def __init__(self):
further_extract()
class ThingyTypeB:
def __init__(self):
further_extract()
>
> Otherwise, you can change the class of an object, even within its own
> methods:
And then I just call __init__ method?
>
> class MyNumbers(object):
> def __init__(self, n = 0):
> self.original_value = n
> self.__class__ = BigNumbers if n> 100 else SmallThing
self.__init__()
>
> class BigNumbers(MyNumbers):
def __init__(self):
> size = 'big'
self.newvalue = self.original_value * y
>
> class SmallNumbers(MyNumbers):
def __init__(self):
> size = 'small'
self.newvalue = self.original_value * x
>
> Hope this helps.
Yes, thanks!
--
Yves. http://www.SollerS.ca/
http://blog.zioup.org/
More information about the Python-list
mailing list