Exemple #1
0
		return f"{self.name} the Dog"

	def set_name(self, name):
		self.name=name

#Creating an instance and accessing attributes
dog = Dog('Jack', 6)
print(f"{dog.name} is {dog.age} years old.")

#Modifying attributes
dog.age=12
print(f"{dog.name} is {dog.age} years old.")

#Calling methods
dog.sit()
dog.roll_over()

#Using a getter and setter
dog.set_name('Clover')
print(dog.get_long_name())

#How to import classes
#Whole module
import classes
#Single class to namespace
from classes import Dog
#Multiple classes to namespace
from classes import Animal, Dog
#All classes directly to namespace
from classes import *
#Alias
Exemple #2
0
#The program is just used as practice problems in chapter 9 of 
#Python Crash Course, Eric Matthes
#classes
#by Jason Delancey
from classes import Dog, HotDog

my_dog = Dog('willie', 6)
print("My dog's name is " + my_dog.name.title() + ".")
print("My dog is " + str(my_dog.age) + " years old.")
my_dog.sit()
my_dog.roll_over()
print()
your_dog = Dog('Jamie', 4)
print("Your dog's name is " + your_dog.name.title() + ".")
print("Your dog is " + str(your_dog.age) + " years old.")
your_dog.sit()
your_dog.roll_over()
print()

my_hotdog = HotDog("Simon", 5)
my_hotdog.sit()
my_hotdog.roll_over()