Object-Oriented Python

Use keyword class to start a class. Class names are capitalized.

Class can have some properties.

Create some instances of a class Student

Add some methods to a class

All methods have to contain at least one parameter, self which refers to a instance of the class


  • Runs when create an instance of a class
  • Doesn’t need to return anything
  • Can have default values

  • setattr(object, attribute, value): object.attribute = value

Use dictionary unpacking in __init__

  • Commonly called kwargs
  • Dictionary unpacking needs two starts **
  • dict.get(key[, default])

    class Student: def init(self, **kwargs): self.gender = kwargs.get(‘gender’, ‘other’) self.school = kwargs.get(‘school’, ‘Best University’) self.grade = kwargs.get(‘grade’, 100)

Use a written class

  • The class Student is in a file called student.py.
  • Need from and import keywords


CollegeStudent inherits from Student:

CollegeStudent has all the properties and methods that Student has.


Controls how an object is printed. Similar to description() in Swift.

  • __class__: returns the class of the instance
  • __name__: name of the module (class)

Leave a Reply

Your email address will not be published. Required fields are marked *