how to declare class in python code example
Example 1: create and use python classes
class Mammal:
def __init__(self, name):
self.name = name
def walk(self):
print(self.name + " is going for a walk")
class Dog(Mammal):
def bark(self):
print("bark!")
class Cat(Mammal):
def meow(self):
print("meow!")
dog1 = Dog("Spot")
dog1.walk()
dog1.bark()
cat1 = Cat("Juniper")
cat1.walk()
cat1.meow()
Example 2: classes in python
class Foo:
def __init__(self):
self.definition = Foo!
def hi():
Example 3: python class
class Animal(object):
def __init__(self, species, price):
self.species = species
self.price = price
def overview(self):
print(f"This species is called a {self.species} and the price for it is {self.price}")
class Fish(Animal):
pass
salmon = Fish("Salmon", "$20")
salmon.overview()
dog = Animal("Golden retriever", "$400")
dog.overview()
Example 4: how to define a class in python
class a_class:
def __init__(self, input1):
self.__input1 = input1
def return_input(self):
return self.__input1
a_class_object = a_class("input string")
print(a_class_object.return_input())