Hi Irit,
In my case, the code structure is as below.
I'm writing a city traffic simulator which includes roads and cars.
Cars have different states, MovingCar, IdleCar, ParkingCar...
A car can move between different states and it keeps the same information.
My solution to this was,
Having a different class that captures the generic functions for each state.
Why writing a module does not work has 2 parts, they may be due to my
insufficient experience with python but I'm switching between the states as
their API's are executed. Something like the code sample below
class AA:
@classmethod
def greet(cls, A_instance):
print("Hello", A_instance.name)
class BB:
@classmethod
def greet(cls, A_instance):
print("Hi", A_instance.name)
class A:
def __init__(self, state, name):
self.state = state
self.name = name
def greet(self):
self.state.greet(self)
def switchAA(self):
self.state = AA
def switchBB(self):
self.state = BB
if __name__ == "__main__":
obj = A(AA, "alperen)
obj.greet()
obj.switchBB()
obj.greet()
The output is:
Hello alperen
Hi alperen
I believe this minimal example captures the semantics that I'm working on. I
thought that AA and BB classes may be classified as a special type of class.
My regards,
Alperen
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/VWXTAI2AGMPYKRQUZPBHBNC2G7KDNMXB/
Code of Conduct: http://python.org/psf/codeofconduct/