塞特犬和盖特斯蟒蛇

val*_*ale 3 python python-2.7

我环顾四周,但我尝试的一切似乎都没有得到任何结果.我只需要为一个将数字转换为字符串而反之亦然的类设置自定义setter和getter.因为python不支持我使用字典.

class Ship(object):
    def __init__(self, type):
        self.fixed = define_direction()

        self.row = random_row(board, self)
        self.col = random_col(board, self)
        self.type = self.set_type(type)
        print "Type:", str(self.type)

    def set_type(self, type):
        return {
            0 : "Patrol boat",#2
            1 : "Destroyer",#3
            2 : "Submarine",#3
            3 : "Battleship",#4
            4 : "Aircraft carrier",#5
            }.get(type, "Patrol boat") 
    def get_type(self):
        return {
            "Patrol boat" : 0,#2
            "Destroyer" : 1,#3
            "Submarine" : 2,#3
            "Battleship" : 3,#4
            "Aircraft carrier" : 4,#5
              }.get(self.type, 0) 
    def __repr__(self):
        return "Ship. Type: ", self.get_type()
Run Code Online (Sandbox Code Playgroud)

不确定这self.type = self.set_type(type)是合法的,但它似乎是从类内部调用函数的唯一方法.

__init__(self, type)- >"type"作为数字传递,它应该被转换并存储为字符串,而不是在调用getter时重新转换为数字.(也许有更好的方法 - 使用外部字典进行转换并只存储数字..?

  • PS1:调用外部函数random_row(board, self)random_col传递它是否合法,self即使它没有正确初始化,还是应该将该函数作为另一个setter移动到类中?

  • PS2:打电话__repl__:

    def __repr__(self):
      return "Ship. Type: ", str(self.get_type()), str(self.type())
    
    Run Code Online (Sandbox Code Playgroud)

    收益:

    Traceback (most recent call last):
      File "/Users/xx/Documents/python/battleship/battleship.py", line 85, in <module>
        print ship.__repr__()
      File "/Users/xx/Documents/python/battleship/battleship.py", line 74, in __repr__
        return "Ship. Type: ", str(self.get_type()), str(self.type())
    TypeError: 'str' object is not callable
    
    Run Code Online (Sandbox Code Playgroud)

    只打电话给它 get_type()

    def __repr__(self):
        return "Ship. Type: ", str(self.get_type())
    
    Run Code Online (Sandbox Code Playgroud)

    收益:

    Type: Submarine
    ('Ship. Type: ', '2')
    
    Run Code Online (Sandbox Code Playgroud)

希望它足够清楚.

Mar*_*ers 5

您可以使用@property装饰器来管理您的type属性:

class Ship(object):
    def __init__(self, type):
        self.fixed = define_direction()

        self.row = random_row(board, self)
        self.col = random_col(board, self)
        self.type = type
        print "Type:", str(self.type)

    @property
    def type(self):
        return {
            "Patrol boat": 0,
            "Destroyer": 1,
            "Submarine": 2,
            "Battleship": 3,
            "Aircraft carrier": 4,
        }.get(self._type, 0) 

    @type.setter
    def type(self, type):
        self._type = {
            0: "Patrol boat",
            1: "Destroyer",
            2: "Submarine",
            3: "Battleship",
            4: "Aircraft carrier",
        }.get(type, "Patrol boat") 

    def __repr__(self):
        return "Ship. Type: " + self._type
Run Code Online (Sandbox Code Playgroud)

__repr__应该总是返回一个字符串; 你回来了一个元组.您的错误是由您的self.type()电话引起的; 因为self.type在代码中存储了一个字符串,所以你试图将该字符串视为可调用的.

可以调用其他函数(在类之外)__init__; 它只是你的实例上的另一种方法,只考虑具有和尚未设置的属性.但是,如果函数依赖于信息self并且在类外没有用处,我会将它移到带有_前缀的类中,以指示它是类实现的内部.