I want to create a class in python, which should work like this:
Data assigned, maybe bound to a variable (eg a = exampleclass(data)
or just exampleclass(data)
)
Upon being inserted data, it should automatically determine some properties of the data, and if some certain properties are fullfilled, it will automatically...
... change class to another class
The part 3 is the part that i have problem with. How do i really change the class inside of the class? for example:
If I have two classes, one is Small_Numbers
, and the other is Big_numbers
; now I want any small_number
smaller than 1000 to be transferred into a Big_number
and vice versa, testcode:
a = Small_number(50)
type(a) # should return Small_number.
b = Small_number(234234)
type(b) # should return Big_number.
c = Big_number(2)
type(c) # should return Small_number.
Is this possible to do?
Why not using a factory method? This one will decide which class to instanciate depending on the passed data. Using your example:
def create_number(number):
if number < 1000:
return SmallNumber(number)
return BigNumber(number)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With