30. Classes & Objects - tomaslt99/Python-language-tutorials GitHub Wiki

Classes & Objects


Code:

File student.py
# create a class "Student", defines what is a student. Class is like an template.
class Student_class:

  # initialize function "__init__"
  # (self, name, major, gpa, is_on_probation) define what attributes a student should
 have.
  def__init__(self, name, major, gpa, is_on_probation):
   # Create objects:
   self.name = name
   self.major = major
   self.gpa = gpa
   self.is_on_probation = is_on_probation
main.py file

# Object is actual studen (real thing, like an item)
from student import Student_class

# Create a student
student1 = Student_class("Mike","Business", 3.1, False)

print(student1.name, student1.major, student1.gpa, student1.is_on_probation)