MishaVreeken / Battleship

0 stars 0 forks source link

unused code #1

Open Jasperras opened 3 years ago

Jasperras commented 3 years ago

https://github.com/MishaVreeken/Battleship/blob/58f51c4868e97614958154cbba5fc67d2d2ccc93/battleship.py#L7 is unused. What is the purpose?

jwizzle commented 3 years ago

To add to this, why keep a shipname at all? If you ever want to check what kind of ship a specific object is it's better to use built-ins. Python already has a lot of ways to compare objects. Also have a look at the eq magic function you can implement for classes.

eg.

class Ship:
    ship_available = True

    def __init__(self, ship_name, length, number):
        self.name = ship_name
        self.length = length
        self.number = number

    def rotate(self, horizontal, vertical):
        self.horizontal = horizontal
        self.vertical = vertical

class Carrier(Ship):
        # Notice how you don't even need any arguments here the way you use it
    def __init__(self):
                # Notice the type(self).__name__ here actually is 'Carrier' already
        super().__init__(ship_name=type(self).__name__, length = 5, number = 1) 
        self.number -= 1 
        if self.number < 1:
            Ship.ship_available = False

schip = Carrier()
print(schip.name)

if isinstance(schip, Carrier):
    print("Schip is een Carrier")
$ python3 vechtschip.py
Carrier
Schip is een Carrier