python - object oriented program
- Details
- Last Updated: Saturday, 12 September 2020 13:34
- Published: Saturday, 29 August 2020 08:37
- Hits: 569
Object Oriented Programming:
Classes, objects, etc are part of Object Oriented prgramming (OOP) in python, which isn't really needed to learn. You can do almost anything with regular programming in python, without using OOP at all. So, this section is mostly optional. Many times you need to know OOP in pyhton, so that you can understand python pgms written in OOP (A lot of new large scripts in Workplace are increasingly written in OOP by cad folks).
class: an object that defines a set of attributes.
ex:
class Employee: => creates new class called Employee
'Common base class for all employees'
empCount = 0 => class var which is shared among all inst of this class. This can be accessed as Employee.empCount from inside the class or outside the class.
#The first method __init__() is a special method, which is called class constructor or initialization method that Python calls when you create a new instance of this class.
def __init__(self, name, salary):
self.name = name
self.salary = salary
Employee.empCount += 1
#declare other class methods like normal functions with the exception that the first argument to each method is self. Python adds the self argument to the list for you; you do not need to include it when you call the methods.
def displayCount(self):
print "Total Employee %d" % Employee.empCount
"This would create first object of Employee class"
emp1 = Employee("Zara", 2000) => create instances of a class. calls ___init__ method
"This would create second object of Employee class"
emp2 = Employee("Manni", 5000) => we can change any attr as follows: emp2.age=8
emp1.displayEmployee()
tot_emp = Employee.empCount
overloading, inheritence etc are allowed with classes.
Functions vs methods:
Now that we have seen both functions and method, we may wonder what's the difference b/w the two.
Functions: As we saw earlier, functions may be inbuilt or user defined, or we import them from various modules. We call func by providing optional args, and they may optionally return a value on exit.
ex: ans = log(a) => This is an inbuilt "log" function.
Methods: As we saw above, methods are OO concept, and they are associated with objects/classes. They are same as functions, and can have optional args, return value etc.
ex: arr_shape = my_array.shape() => Here my_array is an object of type array, and calls a method named shape() on that object. arr_shape stores the tuple returned. Here shape() has no args, but it can have optional args as for method my_array.redshape(-1), etc.
Differences: Methods serve same purpose as function, except that they work implicitly on object on which they are called. Also, method is only accessible by objects in that class, i.e an object that doesn't belong to the class for which that method is defined, then that method won't work on that object. In short, method is a function that belongs to an object, not everyone can use it.
NOTE: Some methods may modify the input object, since they may operate on it directly. ex: my_arry.sort() sorts the my_array object in place, while np.sort(my_array) copies my_array, sorts it and then returns this copy, so that the original my_array remains intact. However, most methods don't modify the input object passed to it, so there is usually no diff b/w method and function.They operate and return result in new object or variable.
Module method vs Module Function: With most modules, you will see that they provide both function and method equivalent for operating on an object. We could use either one as per our convenience. However, some modules may not provide equiv method for a function. Also, there are many in built functions for which there is no module equivalent, so looks like function is the more generic and complete implementation?? Most tutorials that you find online will provide tutorials on modules, and their usage, but will mix methods and functions, for some operation they will use methods while for some they will use Functions. You should consult python documentation for that module to get both method and function equivalent for those operations.
In NumPy module, we have various functions and methods for same operation.
Transpose: transpose of a matrix has function transpose(), as well as method T. We could use either one, however method is more convenient to use in this case, as it's shorter to write.
1. function: new_arr = np.transpose(Arr) => Here transpose is a function with arg Arr, which is an array
2. method: new_arr = Arr.T => Here Arr array is an object, on which the method T is working to transpose it (T method is for transpose).
Median: No method exists for median in NumPy, although for mean, both method and function equivalent exist. Look in documentation to see what exists.
1. function: y = np.median(my_arr) => returns mean of array object
2. method: y = my_arr.median() => This returns an error "AttributeError: 'numpy.ndarray' object has no attribute 'median'".