############## ## Notice that isinstance() takes two
arguments, an object and a class.
class Dog:
print(isinstance(miles, Bulldog)) # False
species = "Canis familiaris"
## More generally, all objects created from a
def __init__(self, name, age): child class are
[Link] = name ## instances of the parent class, although they
[Link] = age may not be
def __str__(self): ## instances of other child classes.
return f"{[Link]} is {[Link]} years old" print([Link]("wooo")) # Buddy barks:
wooo
def speak(self, sound):
print([Link]()) # Miles says Arf
return f"{[Link]} barks: {sound}"
print([Link]("Grrr")) # Miles says Grrr
class JackRussellTerrier(Dog):
# changes to the parent class automatically
def speak(self, sound="Arf"): propagate to child
return f"{[Link]} says {sound}" #classes as long as the attribute or method
being changed isn’t
class Dachshund(Dog):
#overridden in the child class.
pass
#########################
class Bulldog(Dog):
class Dog:
pass
species = "Canis familiaris"
miles = JackRussellTerrier("Miles", 4)
def __init__(self, name, age):
buddy = Dachshund("Buddy", 9)
[Link] = name
jack = Bulldog("Jack", 3)
[Link] = age
jim = Bulldog("Jim", 5)
def __str__(self):
print(miles) # Miles is 4 years old
return f"{[Link]} is {[Link]} years old"
print([Link]) # Canis familiaris
def speak(self, sound):
print(type(miles)) # <class
'__main__.JackRussellTerrier'> return f"{[Link]} barks: {sound}"
print(isinstance(miles, Dog)) # True class JackRussellTerrier(Dog):
def speak(self, sound="Arf"): # changes to the parent class automatically
propagate to child
return super().speak(sound) # parent class
format of speak is used #classes as long as the attribute or method
being changed isn’t
class Dachshund(Dog):
#overridden in the child class.
pass
#########################
class Bulldog(Dog):
class Dog:
pass
species = "Canis familiaris"
miles = JackRussellTerrier("Miles", 4)
def __init__(self, name, age):
buddy = Dachshund("Buddy", 9)
[Link] = name
jack = Bulldog("Jack", 3)
[Link] = age
jim = Bulldog("Jim", 5)
def __str__(self):
print(miles) # Miles is 4 years old
return f"{[Link]} is {[Link]} years old"
print([Link]) # Canis familiaris
def speak(self, sound):
print(type(miles)) # <class
'__main__.JackRussellTerrier'> return f"{[Link]} says {sound}"
print(isinstance(miles, Dog)) # True class GoldenRetriever(Dog):
## Notice that isinstance() takes two def speak(self, sound="Bark"):
arguments, an object and a class.
return super().speak(sound)
print(isinstance(miles, Bulldog)) # False
buddy = GoldenRetriever("Buddy", 9)
## More generally, all objects created from a
child class are print([Link]()) #Buddy says Bark
## instances of the parent class, although they ####################
may not be Tkinter child window (using class)
## instances of other child classes. from tkinter import Button, Tk, Toplevel
print([Link]("wooo")) # Buddy barks: class App(Tk):
wooo
def __init__(self):
print([Link]()) # Miles says Arf
super().__init__()
print([Link]("Grrr")) # Miles says Grrr
[Link]('300x200')
[Link]('Main Window')
# place a button on the root window
Button(self,
text='Open a window',
command=self.open_window).pack(expand=Tr
ue)
def open_window(self):
window = Window(self)
window.grab_set()
class Window(Toplevel):
def __init__(self, parent):
super().__init__(parent)
[Link]('300x100')
[Link]('Toplevel Window')
Button(self,
text='Close',
command=[Link]).pack(expand=True)
if __name__ == "__main__":
app = App()
[Link]()