Class Definition

Object Oriented Programming (OOP)

  • We need to write meaningful OOP codes, merely having classes doesn’t mean it’s oop.

Defining a class

  • Class: defined by class ClassName
class Student():
    pass
  • Define variables, functions/methods

  • To use a class, we need to instantiate it first

  • See examples below

# define a class
class Student():
    name = ""
    age = 0

    def print_file():
        print("name: {} \n age: {}".format(name, str(age)))
# instantiate a class
student = Student()
  • Unlike in other languages, e.g. javascript, we don’t need keyword New to instantiate a class
student.print_file()
---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

<ipython-input-9-dd9e0e00a833> in <module>()
----> 1 student.print_file()


TypeError: print_file() takes 0 positional arguments but 1 was given

It raises an error about taking positional arguments.

  • Writing methods in class is different from writing functions in modules
  • self is needed in all class methods
# re-write the class
class Student():
    name = ""
    age = 0

    def print_file(self):
        print("name: {} \n age: {}".format(name, str(age)))

student = Student()
student.print_file()
---------------------------------------------------------------------------

NameError                                 Traceback (most recent call last)

<ipython-input-10-9c60be204878> in <module>()
      8
      9 student = Student()
---> 10 student.print_file()


<ipython-input-10-9c60be204878> in print_file(self)
      5
      6     def print_file(self):
----> 7         print("name: {} \n age: {}".format(name, str(age)))
      8
      9 student = Student()


NameError: name 'name' is not defined

It raises another error that “name is not defined”

  • we need self. to refer a variable in class
# re-write the class
class Student():
    name = ""
    age = 0

    def print_file(self):
        print("name: {} \n age: {}".format(self.name, str(self.age)))

student = Student()
student.print_file()
name:
 age: 0
  • class is used mainly to encapsulate varialbes and methods

  • class method requires self in argument

  • class is only used to define/describe attributes or behavior, it’s not responsible for running. Don’t run the class in the same module. Keep one class definition in one module; write calling class in another module

Import a Class

  • Define Student class in module c1

# module c1.py

class Student():
    name = ""
    age = 0

    def print_file(self):
        print("name: {} \n age: {}".format(self.name, str(self.age)))
  • Calling and using student in module c2

# module c2.py

from c1 import Student
student = Student()
student.print_file()

Method vs. Function

  • Mostly method is function

  • method: is an OOP concept, it’s more on the design level

  • function: is process oriented and it’s more relevant to the running program/process

  • inside a class, call it method; inside a module, call it function

  • No need to differentiate them on purpose