Chapter 8: Object-oriented programming

Object-oriented programming in Python

face Josiah Wang

You should have gotten the point by now that everything is an object in Python.

Here is a reminder of how to define a class and instantiate a class instance.

1
2
3
4
5
6
7
class Person:
    pass

manager = Person()
print(type(manager)) # <__main__.Person object at 0x7fe70c878c10>
print(isinstance(manager, Person)) # True
print(isinstance(manager, object)) # True

Class instantiation

While the general OOP concepts are the same as in Java, there are many specific details in Python where things are implemented differently. Some of these requires a different way of thinking from what you have been exposed to in Java.

Before you start defining classes for everything, if all you really need is a struct, then perhaps you should consider using a dict instead of a class?