class Person:
# instance attributes
def __init__(self):
pass
# instantiate an object
obj = Person()
person.py
- Next line after
class
defined we create a method start with def
keyword. In Python after def
will be refer as function
but if it is under a class
object it known as method. In simpler way to say that classes define functions called methods, which identify the behaviors and actions for an obejct.
- Inside the
class
, and __init__
method is a special built-in method in Python or we can call it as initializer that can be used later to instantiate an object.
- Inside the
__init__()
which we expanded a ()
parenthesis after __init__
as compared like above that can be put any number of parameters. However it will always first takes one argument variable called self
that refers to the object itself.
pass
is a placeholder used in Python to indicate some python code will be taking over.
Now let think about the attributes of Person
class. Usually when we talk about a person as individual is should have a name, age, gender, status and more. For the sake of simplicity we just put name
and age
for a start as attributes to class Person
. It is time to give the atributes to Person
class.
class Person:
# instance attributes
def __init__(self, name, age):
self.name = name
self.age = age
# instantiate an object
obj = Person()
person.py
Instantiate-an-object-attributes
Since we have created our instance attributes name
and age
. It is time to create two object
s with difference name
and age
as an examples.
class Person:
# instance attributes
def __init__(self, name, age):
self.name = name
self.age = age
# instantiate an object
obj = Person()
# instantiate two objects 'Joe' and 'Jane'
joe = Person("Joe", 23)
jane = Person("Jane", 21)
person.py
Next, how to print object
attributes
Using .
dot-notation to print object attributes
class Person:
# instance attributes
def __init__(self, name, age):
self.name = name
self.age = age
# instantiate an object
obj = Person()
# instantiate two objects 'Joe' and 'Jane'
joe = Person("Joe", 23)
jane = Person("Jane", 21)
# print object name using `.` or dot-notation
joe.name # output - 'Joe'
joe.age # output - 9
person.py
Core-OOP
There are four core of Object-Oriented-Programming
or better known as OOP
.
- Abstraction
- Encapsulation
- Inheritance
- Polymorhism
Different-between-abstraction-vs-encapsulation
Main different between abstraction and encapsulation is
- Encapsulatin were used to do
information hiding
or data privacy
- Abstraction were used to make
implementation hiding
or method privacy
A simple OOP inheritance example
class Pet(object):
def __init__(self, name, species):
self.name = name
self.species = species
def getName(self):
return self.name
def getSpecies(self):
return self.species
def __str__(self):
return "%s is a %s" % (self.name, self.species)
class Dog(Pet):
def __init__(self, name, chases_cats):
Pet.__init__(self, name, "Dog")
self.chases_cats = chases_cats
def chasesCats(self):
return self.chases_cats
class Cat(Pet):
def __init__(self, name, hates_dogs):
Pet.__init__(self, name, "Cat")
self.hates_dogs = hates_dogs
def hatesDogs(self):
return self.hates_dogs
pets.py
Source-reference-with-link
Useful reference: