oop - Selective exposure of methods and variables of one class to another in Python | (interfaces?) -
I'm trying to create a simple robot simulation that runs in 2-D world using PagMem. The obstacles in the world are the only contact sensors in the robot, so it can only understand a few if it is linked to the boundaries of the world or its obstacles. Therefore, the detection of conflict is of utmost importance here.
I have two major classes World and Robot. World class contains information about the geometry of the world and includes a list of obstacles in it. Robot class contains information about robot geometry and it The current situation in the world is. I believe, (but I'm not sure) that the robot should be involved in the world level because it is part of the world. Robots have their own way of displaying and moving, which changes their position in the world. But to find out the confrontation, I have knowledge of the world like its limitations and a list of obstacles.
Now, I could have used my simple simulation by making a world class example a member of the robot class. In this way, my robot gets information about the world and I happily find out the confrontation. But, it does not please me.
Because, I would like to extend simulations with other robots and things in the world, which I do not want to display robots (I am just experimenting with various AI algorithms). In the future, I would like to try something in which the robot has 0 knowledge of the world and can be obtained by exploring it.
If it was Java, then I call the interface (robot world knowledge) which will implement the world level and pass it to the robot class. This interface will have a selective knowledge of the world that will use robots.
I do not know how to do this in Python. I tried Googling "Interface in Python" but could not find a suitable example. Most answers show that interfaces are not necessary in Python.
What I have assumed may be wrong. Please help me
Thanks in advance
shahensha
Good in Python The interface is framework - but it also gives one to everyone to create a quick and dirty thing - like an object proxy that can highlight the properties of the desired methods and an underlying object.
This can be done in class to reduce a proper and: __getattribute __ method:
class interface (object): def __init __ (self, real_object, interface): ifinstance (interface, str): interface = interface.split () self.inobject = real_object interface. Validate (self) def __getattribute __ (auto, attr): # Retrieve the actual characteristics of yourself, excluding # general attribute mechanisms: interface = object .__ getattribute __ (self, "interface") real_object = object .__ getattribute __ (Self, "object") in the etter interface: Return Gateet (real_object, attr) attributeError def increases valid (self): tuitium for interface at object = object .__ ge attribute __ (self, "interface") real_object = Object .__ getattribute __ (self, "object"): try: getattr (real_obje Ct, attr): Exclude AttributeError: Increase ValueError ("The passed object does not conform to the given interface")
& gt; & Gt; & Gt; Category A (object): ... a = 1 ... b = 2 ... c = 3 ... & gt; & Gt; & Gt; A = A ()> & Gt; & Gt; B = interface (A, "ABC")> & gt; & Gt; & Gt; B = interface (A, "ABC")> & gt; & Gt; & Gt; B1A1 & gt; & Gt; & Gt; B.B. 2 & gt; & Gt; & Gt; B C. Traceback (Most recent call end): File "& lt; stdin>", line 1, & lt; Module & gt; File "& lt; stdin & gt;", in line 16, the __getattribute__ attribute email & gt; & Gt; Class C (Object): Pass ... & gt; & Gt; & Gt; Interface (c (), "A") Traceback (most recent call final): File "& lt; stdin>", line 1, & lt; Module & gt; html
Comments
Post a Comment