1class uneclasse():
2 def __init__(self):
3 pass
4 def something(self):
5 pass
6xx = uneclasse()
7xx.something()
1
2class Box(object): #(object) ending not required
3 def __init__(self, color, width, height): # Constructor: These parameters will be used upon class calling(Except self)
4 self.color = color # self refers to global variables that can only be used throughout the class
5 self.width = width
6 self.height = height
7 self.area = width * height
8 def writeAboutBox(self): # self is almost always required for a function in a class, unless you don't want to use any of the global class variables
9 print(f"I'm a box with the area of {self.area}, and a color of: {self.color}!")
10
11greenSquare = Box("green", 10, 10) #Creates new square
12greenSquare.writeAboutBox() # Calls writeAboutBox function of greenSquare object
1class Student:
2 def __init__(self, id, name, age):
3 self.name = name
4 self.id = id
5 self.age = age
6
7 def greet(self):
8 print(f"Hello there.\nMy name is {self.name}")
9
10 def get_age(self):
11 print(f"I am {self.age}")
12
13 def __add__(self, other)
14 return Student(
15 self.name+" "+other.name,
16 self.id + " "+ other.id,
17 str(self.age) +" "+str(other.age))
18
19p1 = Student(1, "Jay", 19)
20p2 = Student(2, "Jean", 22)
21p3 = Student(3, "Shanna", 32)
22p4 = Student(4, "Kayla", 23)
23
24
25result = p1+p3
1A class is a block of code that holds various functions. Because they
2are located inside a class they are named methods but mean the samne
3thing. In addition variables that are stored inside a class are named
4attributes. The point of a class is to call the class later allowing you
5to access as many functions or (methods) as you would like with the same
6class name. These methods are grouped together under one class name due
7to them working in association with eachother in some way.
1class car:
2 def __init__(self, model, color):
3 self.model = model
4 self.color = color
5
6tesla = car("model 3", "black")
7