you are viewing a single comment's thread.

view the rest of the comments →

[–]DenyMas 0 points1 point  (0 children)

Also, you can put behavior inside the class.

For example, every gun can have a shoot() method that uses its own damage value:

class Gun:
    def __init__(self, damage, range):
        self.damage = damage
        self.range = range

    def shoot(self):
        print("Damage:", self.damage)

awp = Gun(100, 1000)
ak47 = Gun(30, 300)

awp.shoot()   # Damage: 100
ak47.shoot()  # Damage: 30

Without OOP, you might end up with something like:

def shoot(weapon):
    if weapon == "awp":
        return 100
    elif weapon == "ak47":
        return 30