how do python classes work 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()
Example 2: python class
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def myfunc(self):
print("Hello my name is " + self.name +".")
p1 = Person("Victor", 24)
p1.myfunc()
Example 3: how to declare a class in python
class ClassName(object):
def __init__(self, var1=0, var2):
self.age = var1
self.name = var2
def otherFunction(self):
Example 4: 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 5: how to use class's in python
class person:
name = "jake"
age = 13
x = vars(person)
print(x)