Reputation: 1426
I've looked around but everything I tried didn't seem to get any result. I just need to set a custom setter and getter for a class that converts a number to string and viceversa. Since python doesn't support which I'm using a dictionary.
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()
Not really sure that self.type = self.set_type(type)
is legit, but it seems the only way to call a function from inside the class.
In __init__(self, type)
-> "type" gets passed as a number and it should get converted and stored as a string, than should get reconverted to a number while the getter is called. (maybe there's a better way to do it - using external dictionary to do the conversion and just store the number..?
PS1: Is it legit to call an external function random_row(board, self)
and random_col
passing self
even if it's not properly initialized or should I move the function inside the class as another setter?
PS2: calling __repl__
:
def __repr__(self):
return "Ship. Type: ", str(self.get_type()), str(self.type())
returns:
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
Calling it only on get_type()
def __repr__(self):
return "Ship. Type: ", str(self.get_type())
returns:
Type: Submarine
('Ship. Type: ', '2')
Hope it's clear enough.
Upvotes: 3
Views: 1149
Reputation: 1125398
You can use a @property
decorator to manager your type
attribute:
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
Your __repr__
should always return a string; you were returning a tuple instead. Your error was caused by your self.type()
call; since self.type
stores a string in your code, you were trying to treat that string as a callable.
It is fine to call other functions (outside of the class) from __init__
; it is just another method on your instance really, just take into account what attributes have and haven't been set yet. However, if the function relies on information on self
and has no uses outside the class, I'd move it into the class with a _
prefix to indicate that it is internal to the class implementation.
Upvotes: 5