How does one implement in python the equivalent of a C++ class member that returns a pointer to another class? -
with background in c++, trying learn python , trying understand how write code without pointers. in specific example, know how implement following c++ code in python:
class room: public mapsite { public: room(int roomno); mapsite* getside(direction) const; // direction enum void setside(direction, mapsite*); virtual void enter(); // inherited method mapsite class private: mapsite* sides(4); int roomno; } the above example comes
desgin patterns: elements of reusable object-oriented software
i want know how implement above in python, getside() function , sides variable utilize pointer class. thank you.
edit:
what wanted know originally, without realizing @ first, how c++ class provided implemented in pythonic way. realize broad of request, though of answers helpful me , set me on right path.
some differences c++: in python, object, , variables contain references objects, rather objects themselves. if want object of class have attributes, assign them in constructor named __init__. each member function including constructor has first parameter self. whenever call members f or use attributes you'll have add 'self', self.f () , self.a. note in fact working "dereferenced pointers" (references). 1 of main pitfalls c++ programmers conclude can return value in function parameter since reference. if assign function parameter inside function, after reference newly assigned object. object passed caller not altered. advise work through python tutorials , pay attention cython, since allow combine c++ , python knowledge.
your program like:
class room (mapsite): def __init__ (self, room_no): self._room_no = room_no self._map_site = none def get_side (self, direction): return ... def set_side(self, direction, map_site): ... # no need define enter since inherited
Comments
Post a Comment