Python Basics - 014: Classes and Objects
Object oriented programming is something that I identify mostly with java as it is the only other language that I know. Let's see how python handles classes and objects.
Classes are like blueprints for creating objects, classes usually contain a combination of variables and functions.
If you take fish as an object, then the fish may have descriptors about it such as size, weight, color, species, etc. these are analogous to member variables of an object. The fish may also have functions like swim, eat, breathe, etc, these are analogous to member functions.
Let's get started with the syntax for defining classes
class Fish:
size = "large"
color = "white"
def swim(self):
print("the", self.size, self.color, "fish is swimming")
goldfish = Fish()
goldfish.swim()
goldfish.size = "small"
goldfish.color = "gold"
goldfish.swim()
you should get the below output
the large white fish is swimming
the small gold fish is swimming
What we can see from the example is that
- we can define class using the
class
keyword. - all member methods take by default the
self
argument which passes in the object in which the method is in, so as to access the variables (remember variable's scope?) - member variables have a default value, hence it works without us passing any values to them.
- member variables of an object can be accessed by using the
.
dot operator, same goes for member functions.
We can also initiate a object with some values during the creation of the object, similar to a constructor in java. we use __init__()
function in python. In fact calling Fish()
will internally call Fish.__init__()
automatically if the function is implemented.
Here's the above example, slightly modified:
class MarineCreatures:
size = "large" #class variable
def __init__(self, color):
self.color = color #instance variable
def swim(self):
print("A", self.size, self.color, "marine creature is swimming")
seahorse = MarineCreatures("blue")
seahorse.swim()
output:
A large blue marine creature is swimming
Here the instance variable is initiate when the class is created, however unlike class variables, instance variables are not optional. If you create the object with the following syntax, it will throw an error because the argument number is not matching for the __init__()
function.
Some things to note about objects in python:
- Unlike java, member variables are by default public, and there is no private variables in python.[3]
- We can remove writable member variables at runtime.[4]
- We can add member variables at runtime [6]
- We can define functions inside other functions, although it is not good practice to use it that way[5]
Classes are quite important to how we code in python, even though it is not necessary, I'd like to use object oriented approach to make the project more modular and easily readable.
There are other concepts about classes like inheritance, but we'll see to them later. Next stop is Exception Handling
P.S: all the code used in this tutorial is available in github
References:
- https://docs.python.org/3/tutorial/classes.html#a-first-look-at-classes
- https://docs.python.org/3/tutorial/classes.html#class-and-instance-variables
- https://docs.python.org/3/tutorial/classes.html#private-variables
- https://docs.python.org/3/tutorial/classes.html#instance-objects
- https://docs.python.org/3/tutorial/classes.html#scopes-and-namespaces-example
- https://docs.python.org/3/tutorial/classes.html#odds-and-ends
No comments:
Post a Comment