-1

With a background in C++, I am currently trying to learn Python and am trying to understand how to write code without pointers. In a specific example, I would like to know how to implement the following C++ code in Python:

class Room: public MapSite
{
public:
Room(int roomNo);
MapSite* GetSide(Direction) const;  // Direction is an enum
void SetSide(Direction, MapSite*);
virtual void Enter();    // An inherited method from MapSite class

private:
MapSite* sides(4);
int  roomNo;
}

The above example comes from

Desgin Patterns: Elements of Reusable Object-Oriented Software

I want to know how you would implement the above in Python, specifically the GetSide() function and the sides variable that utilize a pointer to another class. Thank you.

Edit:

What I wanted to know originally, without fully realizing it at first, was how the C++ class I provided could be implemented in a Pythonic way. I now realize that is too broad of a request, though some of the answers were helpful to me and set me on the right path.

Community
  • 1
  • 1
Joe
  • 80
  • 7

4 Answers4

2

The thing is that any variable or attribute is a reference in Python. If you want to store an object as an attribute of a class, just assign it; Python stores the reference.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
1

Some differences with C++: In Python, everything is an object, and variables contain references to objects, rather then the objects themselves. If you want an object of a class to have attributes, assign them in the constructor named __init__. Each member function including the constructor has a first parameter self. Whenever you call members f or use attributes a you'll have to add the 'self', so self.f () and self.a. Note that you in fact are constantly working with "dereferenced pointers" (references). One of the main pitfalls for C++ programmers is to conclude that you can return a value in a function parameter since it is a reference. If however you assign to a function parameter inside a function, it will after that be a reference to the newly assigned object. So the object passed by the caller will not be altered. I advise you to work through some Python tutorials and also pay attention to Cython, since it will allow you to combine your C++ and Python knowledge.

Your program would look something 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 to define enter since it is inherited
Jacques de Hooge
  • 6,750
  • 2
  • 28
  • 45
0

I'll try to explain as much as I can do for each of the lines in your code

class Room: public MapSite {

To declare a class Room which inherits MapSite you can do

class Room(MapSite):

in Python. Here MapSite must also be a class to be inherited.

public: Room(int roomNo);

To declare a constructor in a class, you can use __init__ in Python.

def __init__(self, roomNo)

Now, how can you write enum in Python? You can do that by enum module Please refer : How can I represent an 'Enum' in Python?

Concerning the private or public keywords in C++, you don't need them here directly as such tokens are not defined in Python.

I don't know how much more help you want. It would be better if you could describe more clearly in your question.

Community
  • 1
  • 1
Himanshu Mishra
  • 8,510
  • 12
  • 37
  • 74
0

Good luck with learning Python! I made the switch from C++ about 9 years ago, and nowadays programming in C++ feels like the stone age.

In Python, every time you pass an object around, you pass reference, making pointer obsolete. So your code in Python would simply be:

class Room(MapSite):
    def __init__(self, roomNo):
        self.__roomNo = roomNo
        self.__sides = []
    def GetSide(direction):
        return self.__sides[direction]  # Presumably
    def SetSide(direction, side):
        self.__sides[direction] = side
    def Enter():
        # Your code here...

In Python, all functions are treated as virtual. The double underscore before a member makes it 'private'. The 'this' pointer is explicit in Python, and by convention is called 'self'.

All member variables should be defined in the constructor ('init' function), members defined directly in the class scope are the Python equivalent of C++ static members.

EvertW
  • 1,160
  • 9
  • 18