1class Dog:
2
3 def bark(self):
4 print("Woof!")
5
6 def roll(self):
7 print("*rolling*")
8
9 def greet(self):
10 print("Greetings, master")
11
12 def speak(self):
13 print("I cannot!")
14
15# Creating the Dog class instance and saving it to the variable <clyde>
16clyde = Dog()
17clyde.bark() # --> Woof!
18clyde.roll() # --> *rolling*
19clyde.greet() # --> Greetings, master
20clyde.speak() # --> I cannot!
21
22# Creating another Dog instance
23jenkins = Dog()
24jenkins.bark() # --> Woof!
25jenkins.roll() # --> *rolling*
26# .. And other methods
27# .. Infinite objects can be created this way, all implementing the same methods defined in our class
28
1class Person:#set name of class to call it
2 def __init__(self, name, age):#func set ver
3 self.name = name#set name
4 self.age = age#set age
5
6
7 def myfunc(self):#func inside of class
8 print("Hello my name is " + self.name)# code that the func dose
9
10p1 = Person("barry", 50)# setting a ver fo rthe class
11p1.myfunc() #call the func and whitch ver you want it to be with
1class Person:
2 def __init__(self, name, age):
3 self.name = name
4 self.age = age
5 def myfunc(self):
6 print("Hello my name is " + self.name +".")
7
8p1 = Person("Victor", 24)
9p1.myfunc()
1class Animal(object): # Doesn't need params but put it there anyways.
2 def __init__(self, species, price):
3 self.species = species # Sets species name
4 self.price = price # Sets price of it
5
6 def overview(self): # A function that uses the params of the __init__ function
7 print(f"This species is called a {self.species} and the price for it is {self.price}")
8
9class Fish(Animal): # Inherits from Animal
10 pass # Don't need to add anything because it's inherited everything from Animal
11
12salmon = Fish("Salmon", "$20") # Make a object from class Fish
13salmon.overview() # Run a function with it
14dog = Animal("Golden retriever", "$400") # Make a object from class Animal
15dog.overview() # Run a function with it
1
2class Dog(object):
3 def __init__(self, name, age):
4 self.name = name
5 self.age = age
6
7 def speak(self):
8 print("Hi I'm ", self.name, 'and I am', self.age, 'Years Old')
9
10JUB0T = Dog('JUB0T', 55)
11Friend = Dog('Doge', 10)
12JUB0T.speak()
13Friend.speak()
1class MyClass:
2 """A simple example class"""
3 i = 12345
4
5 def f(self):
6 return 'hello world'
7