What is object oriented programming?
Classes and Object
The two basic concepts in object oriented programming are the class and object.
Class a class defines a new king of thing and how that thing can behave.
Object is a thing with particular set of properties tat behaves how its class tells it to.
# A simple class
class Empty(object):
''' A class that does nothing. '''
pass
# Two object of class Empty
object_one = Empty()
object_two = Empty()
object_one and object_two are variables that point to objects. Both of these these objects point to the class Empty. Like objects, classes point to other classes in a hierarchical fashion and acquire their characteristics. For example, the class Empty points to the class object. Python adds this reference to Empty because we put object in parenthesis after Empty's name when we defined it.
We define the behavior of a class with methods. Methods are functions defined inside a class that can be called to get the object to do something.
# A class with a method
class OneMethod(object):
''' A class that squares and returns the_value it is fed. '''
def square(self, the_value):
return (the_value * the_value)
>>> object_three = OneMethod()
>>> object_three.square(10)
100
We call the OneMethod class the exact same way that we call "sting.upper()" or "list.pop()", and all of the other python builtin methods. The "self" argument in the square function refers to the object the method is being called for. For example, the "self" refers to object_three.
Objects and classes combine functions with data making them more customizable and easier to manage.
# An example of customizability
class Square(object):
def square(self, value):
return(value * value / self.devide)
>>> a = Square()
>>> a.devide = 2
>>> a.square(10)
50.0
>>> b = Square()
>>> b.devide = 4
>>> b.square(10)
25.0
As you can see, we can have different objects referring to the same method, without modifying it. This is very error-proof and allows us to easily manage multiple uses of the function throughout our code.
We define the behavior of a class with methods. Methods are functions defined inside a class that can be called to get the object to do something.
# A class with a method
class OneMethod(object):
''' A class that squares and returns the_value it is fed. '''
def square(self, the_value):
return (the_value * the_value)
>>> object_three = OneMethod()
>>> object_three.square(10)
100
We call the OneMethod class the exact same way that we call "sting.upper()" or "list.pop()", and all of the other python builtin methods. The "self" argument in the square function refers to the object the method is being called for. For example, the "self" refers to object_three.
Objects and classes combine functions with data making them more customizable and easier to manage.
# An example of customizability
class Square(object):
def square(self, value):
return(value * value / self.devide)
>>> a = Square()
>>> a.devide = 2
>>> a.square(10)
50.0
>>> b = Square()
>>> b.devide = 4
>>> b.square(10)
25.0
As you can see, we can have different objects referring to the same method, without modifying it. This is very error-proof and allows us to easily manage multiple uses of the function throughout our code.
Constructor
def __init__(self, *)