python classes examples
Example 1: python classes
class Box(object):
def __init__(self, color, width, height):
self.color = color
self.width = width
self.height = height
self.area = width * height
def writeAboutBox(self):
print(f"I'm a box with the area of {self.area}, and a color of: {self.color}!")
greenSquare = Box("green", 10, 10)
greenSquare.writeAboutBox()
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: classes in python
class Person():
alive = True
def __init__(self, name, age):
self.name = name
self.age = age
def speak(self):
print(f'Hello, my name is {self.name} and my age is {self.age}')
person_one = Person('Sam', 23)
person_one.speak()
==================================================================
>>> 'Hello, my name is Sam and my age is 23'
Example 4: class in python
class LambdaClass:
x = lambda a, b, c, d, e, f: a + b + c + d + e + f
print(x(31231, 312, 312, 31, 12, 31))
print(LambdaClass)
Example 5: class in python
10
<function Person.greet at 0x7fc78c6e8160>
This is a person class