class definition python code example
Example 1: how to make a class in python
class Person:
def __init__(self, _name, _age):
self.name = _name
self.age = _age
def sayHi(self):
print('Hello, my name is ' + self.name + ' and I am ' + self.age + ' years old!')
p1 = Person('Bob', 25)
p1.sayHi() # Prints: Hello, my name is Bob and I am 25 years old!
Example 2: 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 3: python define class
class uneclasse():
def __init__(self):
pass
def something(self):
pass
xx = uneclasse()
xx.something()
Example 4: python class
class Person:#set name of class to call it
def __init__(self, name, age):#func set ver
self.name = name#set name
self.age = age#set age
def myfunc(self):#func inside of class
print("Hello my name is " + self.name)# code that the func dose
p1 = Person("barry", 50)# setting a ver fo rthe class
p1.myfunc() #call the func and whitch ver you want it to be with
Example 5: class python
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
p1 = Person("John", 36)
p1.age = 40
print(p1.age)
---------------------------------------------------------------
40
Example 6: class methods in python
@classmethod
def func(cls, args...)