migrate C/C++ code "typedef struct" to python

i have a question:

I try to programing an ANT algorithm on python but I have a code on C++, and i don't know how to programing this part:

typedef struct {
    int x;
    int y;
} cityType;

typedef struct {
    int curCity;
    int nextCity;
                       //MAX_CITIES
    unsigned char tabu[15];
    int pathIndex;
                       //MAX_CITIES
    unsigned char path[15];
    double tourLength;
} antType;

I programing this, but i am not realy sure

class CityType:
    def __init__(self):
        self.x = arange(MAX_CITIES)
        self.y = arange(MAX_CITIES)

# Class AntType
class AntType:
    def __init__(self):
        self.curCity = arange(MAX_ANTS)
        self.nextCity = arange(MAX_ANTS)
        self.tabu = arange(MAX_ANTS)
        self.pathIndex = arange(MAX_ANTS)
        self.path = arange(MAX_ANTS)
        self.tourLength = arange(MAX_ANTS)

Thanks

Upvotes: 1

Views: 4896

Answers (1)

tiwo
tiwo

Reputation: 3349

There is nothing really wrong with that. But in trying to do a word-by-word translation, you are not using the full power and conveniance that Python offers.

For example, if CityType has only two members, x and y, maybe just a tuple of length two is more pythonic. The NamedTuple provides access to tuple members "by name".

Upvotes: 2

Related Questions