Updating objects
Related code
class Dog: """Represents a dog. attributes: name: str, age: int, energy: int """ def print(self): print(f'Name: {self.name}, Age: {self.age}') def set_info(self, name, age, energy): self.name = name self.age = age self.energy = energy murki = Dog() murki.set_info("Murki", 2, 50)
The play
and sleep
methods are part of the Dog
class,
and they modify the dog's internal state, specifically, its energy
attribute.
So, the Dog
object that calls these method already have an energy
attribute,
the methods simply update its value.
class Dog: ... def play(self, hours): self.energy -= hours * 10 def sleep(self, hours): self.energy += hours * 10
>>> murki.energy 50 >>> murki.play(2) >>> murki.energy 30 >>> murki.sleep(3) >>> murki.energy 60 >>>
Exercises
class Car: """Represents a car. attributes: model: str, fuel: int """ def set_info(self, model, fuel): self.model = model self.fuel = fuel
Define a method, drive, which takes one integer, kilometers, and subtracts 2 * kilometers from fuel.
Create a car, set its attributes, and call drive.
class BankAccount: """Represents a bank account. attributes: owner: str, balance: int """ def set_info(self, owner, balance): self.owner = owner self.balance = balance
Define a method, deposit, which takes one integer, amount, and adds it to balance.
Define a method, withdraw, which takes one integer, amount, and subtracts it from balance.
Create an account, set attributes, and call deposit and withdraw.
class Plant: """Represents a plant. attributes: type: str, water_level: int """ def set_info(self, type, water_level): self.type = type self.water_level = water_level
Define a method, water, which takes one integer, liters, and adds 5 * liters to water_level.
Define a method, dry_day, which takes no arguments and subtracts 10 from water_level.
Create a plant, set attributes, and call water and dry_day.
class Politician: """Represents a politician. attributes: name: str, approval: int """ def register(self, name, approval): self.name = name self.approval = approval
Define a method, give_speech, which takes one integer, impact, and adds it to approval.
Define a method, scandal, which subtracts 20 from approval.
Create a politician, set attributes, and call give_speech and scandal.
class GameCharacter: """Represents a game character. attributes: nickname: str, health: int, level: int """ def setup(self, nickname, health, level): self.nickname = nickname self.health = health self.level = level
Define a method, take_damage, which takes one integer, points, and subtracts it from health.
Define a method, level_up, which adds 1 to level.
Create a character, set its attributes, and call take_damage and level_up.
Exercises
class Student: """Represents a student. attributes: name: str, grade: int """ def set_info(self, name, grade): self.name = name self.grade = grade
Define a method, study, which takes one integer, hours, and adds 1 * hours to grade.
Define a method, skip_class, which takes no arguments and subtracts 5 from grade.
Create a student, set its attributes, and call study and skip_class.
class Athlete: """Represents an athlete. attributes: name: str, stamina: int, strength: int """ def set_info(self, name, stamina, strength): self.name = name self.stamina = stamina self.strength = strength
Define a method, train, which takes one integer, hours, adds 2 * hours to strength, and subtracts hours from stamina.
Create an athlete, set attributes, and call train.
class Book: """Represents a book. attributes: title: str, pages: int """ def add_details(self, title, pages): self.title = title self.pages = pages
Define a method, add_chapter, which takes one integer, extra_pages, and adds it to pages.
Create a book, set its attributes, and call add_chapter.
class BankCard: """Represents a bank card. attributes: holder: str, limit: int """ def initialize(self, holder, limit): self.holder = holder self.limit = limit
Define a method, increase_limit, which takes one integer, amount, and adds it to limit.
Create a card, set attributes, and call increase_limit.
class Bicycle: """Represents a bicycle. attributes: brand: str, gear: int """ def configure(self, brand, gear): self.brand = brand self.gear = gear
Define a method, change_gear, which takes one integer, new_gear, and sets gear to that value.
Create a bicycle, set attributes, and call change_gear.