use class in function python code example
Example 1: python classes
class Student:
def __init__(self, id, name, age):
self.name = name
self.id = id
self.age = age
def greet(self):
print(f"Hello there.\nMy name is {self.name}")
def get_age(self):
print(f"I am {self.age}")
def __add__(self, other)
return Student(
self.name+" "+other.name,
self.id + " "+ other.id,
str(self.age) +" "+str(other.age))
p1 = Student(1, "Jay", 19)
p2 = Student(2, "Jean", 22)
p3 = Student(3, "Shanna", 32)
p4 = Student(4, "Kayla", 23)
result = p1+p3
Example 2: how to use class's in python
class person:
name = "jake"
age = 13
x = vars(person)
print(x)
Example 3: 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())