class Dog:
pass
class Dog:
# Initializer
def __init__(self):
pass
# Instantiating object
ozzy = Dog()
print(ozzy)
<__main__.Dog object at 0x000001DC2AE8F1D0>
class Dog:
# Initializer / Instance Attributes
def __init__(self, nm, ag):
self.name = nm
self.age = ag
class Dog:
# Class Attribute
species = 'mammal'
# Initializer / Instance Attributes
def __init__(self, name, age):
self.name = name
self.age = age
class Dog:
# Class Attribute
species = 'mammal'
# Initializer / Instance Attributes
def __init__(self, nm, ag):
self.name = nm
self.age = ag
# Instantiate the Dog object
ozzy = Dog("Ozzy", 5)
skippy = Dog("Skippy", 6)
# Access the instance attributes
print(ozzy.name, ozzy.age)
print(skippy.name, skippy.age)
print(ozzy.name + " is " + str(ozzy.age) + " year(s) old!")
# Access the class attributes
print(Dog.species)
print(ozzy.species)
print(skippy.species)
# Is Ozzy a mammal?
if ozzy.species == "mammal":
print(ozzy.name, ozzy.species)
Ozzy 5 Skippy 6 Ozzy is 5 year(s) old! mammal mammal mammal Ozzy mammal
class Dog:
# Class Attribute
species = 'mammal'
# Initializer / Instance Attributes
def __init__(self, nm, ag):
self.name = nm
self.age = ag
# instance method
def doginfo(self):
print(self.name + " is " + str(self.age) + " year(s) old.")
# instance method
def speak(self, sound):
return (self.name, sound)
# Instantiate the Dog object
mikey = Dog("Mikey", 6)
ozzy = Dog("Ozzy", 2)
skippy = Dog("Skippy", 12)
filou = Dog("Filou", 8)
# call our instance methods
mikey.doginfo()
print(mikey.speak("Gruff Gruff"))
ozzy.doginfo()
print(ozzy.speak("Bark Bark"))
skippy.doginfo()
filou.doginfo()
Mikey is 6 year(s) old. ('Mikey', 'Gruff Gruff') Ozzy is 2 year(s) old. ('Ozzy', 'Bark Bark') Skippy is 12 year(s) old. Filou is 8 year(s) old.
# Parent class
class Dog:
# Class attribute
species = 'mammal'
# Initializer / Instance attributes
def __init__(self, nm, ag):
self.name = nm
self.age = ag
# instance method
def description(self):
print(self.name + " is " + str(self.age) + " year(s) old!")
# instance method
def speak(self, sound):
print(self.name + " says " + sound)
# Child class (inherits from Dog class)
class RussellTerrier(Dog):
def run(self, speed):
print(self.name + " runs " + speed)
# Child class (inherits from Dog class)
class Bulldog(Dog):
def run(self, speed):
print(self.name + " runs " + speed)
# Child classes inherit attributes and
# behaviors from the parent class
jim = Bulldog("Jim", 12)
jim.description()
jim.speak("Bark Bark!")
skippy = RussellTerrier("Skippy", 8)
skippy.description()
# Child classes have specific attributes
# and behaviors as well
jim.run("slowly")
skippy.run("fast")
Jim is 12 year(s) old! Jim says Bark Bark! Skippy is 8 year(s) old! Jim runs slowly Skippy runs fast
# Parent class
class Dog:
# Class attribute
species = 'mammal'
# Initializer / Instance attributes
def __init__(self, name, age):
self.name = name
self.age = age
# instance method
def description(self):
print(self.name + " is " + str(self.age) + " year(s) old!")
# instance method
def speak(self, sound):
print(self.name + " says " + sound)
# Child class (inherits from Dog() class)
class RussellTerrier(Dog):
def run(self, speed):
print(self.name + " runs " + speed)
# Child class (inherits from Dog() class)
class Bulldog(Dog):
def run(self, speed):
print(self.name + " runs " + speed)
# Child classes inherit attributes and
# behaviors from the parent class
jim = Bulldog("Jim", 12)
jim.description()
# Child classes have specific attributes
# and behaviors as well
jim.run("slowly")
# Is jim an instance of Dog()?
print(isinstance(jim, Dog))
# Is julie an instance of Dog()?
julie = Dog("Julie", 100)
print(isinstance(julie, Dog))
# Is johnny walker an instance of Bulldog()
johnnywalker = RussellTerrier("Johnny Walker", 4)
print(isinstance(johnnywalker, Bulldog))
# Is julie and instance of jim?
print(isinstance(julie, jim))
Jim is 12 year(s) old! Jim runs slowly True True False
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-8-df9ce49686ec> in <module>() 52 53 # Is julie and instance of jim? ---> 54 print(isinstance(julie, jim)) TypeError: isinstance() arg 2 must be a type or tuple of types
# Parent class
class Dog:
# Class Attribute
species = 'mammal'
# Child class (inherits from Dog class)
class SomeBreed(Dog):
pass
# Child class (inherits from Dog class)
class SomeOtherBreed(Dog):
species = 'reptile'
frank = SomeBreed()
print(frank.species)
beans = SomeOtherBreed()
print(beans.species)
mammal reptile