making class and methods in python code example
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: how to create an object in python
class ClassName:
self.attribute_1 = variable_1
self.attrubute_2 = variable_2
def __init__(self, attribute_3, attribute_4):
self.attribute_3 = attribute_3
self.attribute_4 = attribute_4
def method(self):
print("This is a method example.")
object = Object(4, "string")
object.method()