Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

############## ## 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
self.name = name ## instances of the parent class, although they
self.age = age may not be

def __str__(self): ## instances of other child classes.

return f"{self.name} is {self.age} years old" print(buddy.speak("wooo")) # Buddy barks:


wooo
def speak(self, sound):
print(miles.speak()) # Miles says Arf
return f"{self.name} barks: {sound}"
print(miles.speak("Grrr")) # Miles says Grrr
class JackRussellTerrier(Dog):
# changes to the parent class automatically
def speak(self, sound="Arf"): propagate to child

return f"{self.name} 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)
self.name = name
jack = Bulldog("Jack", 3)
self.age = age
jim = Bulldog("Jim", 5)
def __str__(self):
print(miles) # Miles is 4 years old
return f"{self.name} is {self.age} years old"
print(miles.species) # Canis familiaris
def speak(self, sound):
print(type(miles)) # <class
'__main__.JackRussellTerrier'> return f"{self.name} 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)
self.name = name
jack = Bulldog("Jack", 3)
self.age = age
jim = Bulldog("Jim", 5)
def __str__(self):
print(miles) # Miles is 4 years old
return f"{self.name} is {self.age} years old"
print(miles.species) # Canis familiaris
def speak(self, sound):
print(type(miles)) # <class
'__main__.JackRussellTerrier'> return f"{self.name} 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(buddy.speak()) #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(buddy.speak("wooo")) # Buddy barks: class App(Tk):
wooo
def __init__(self):
print(miles.speak()) # Miles says Arf
super().__init__()
print(miles.speak("Grrr")) # Miles says Grrr
self.geometry('300x200')

self.title('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)

self.geometry('300x100')

self.title('Toplevel Window')

Button(self,

text='Close',

command=self.destroy).pack(expand=True)

if __name__ == "__main__":

app = App()

app.mainloop()

You might also like