Object-oriented programming (OOP) is a programming language structure that is based on the concept of 'objects', which are data structures that contain all sorts of wonderful information. Different objects can and will have different data (also known as attributes) and different procedures (also known as methods) to do certain tasks.
Now you might be thinking, "How do we create an object?". Well in Python, objects are instances of classes. So in other words, if you want to create an object, all you have to do is create a class! As we've learned from before, when we create a class, we also need to create the __init__, __str__, __repr__, and __eq__ functions. BUT WAIT!!! These aren't functions! When we write functions inside of a class they are actually called 'methods'.
I guess the best thing about objects is that it can actually save you time and energy from rewriting code. For example, you can create a subclass for the built-in String class and it will inherit ALL the methods that the String class has! Don't see how that saves time? Go ahead and create a class and try to write ALL the SAME methods that the String class has.
Just by doing that, MyStringClass has now inherited ALL the methods that the built-in String class has. Don't believe me? Go ahead and create a class like my MyStringClass and use isinstance(MyStringClass, str). It should return True since MyStringClass is a subclass of String.
No comments:
Post a Comment