Python allow to object oriented programming (OOP). That is make a code writing so fast. Because object oriented programming reuse another code that containt formula and data.
As OOP, main emphasis of Python is on object. Another progamming paradigm, procedure oriented programming stress on function.
Object is set of data (variable) and method (functions) that can work on data. To create an object, programmer create a blueprint, first, then create object. The blueprint is named a class. Then we can make multiple object based on a classs. That is make coding faster, easier to write and read.
Class is reusable object as blue print that contain attribut and methods. A class is a description of an object’s characteristics.
Some book say that class is also an object. So, to prevent confusion, some writer give different name for object, it is instance. Instance is an object that is create based on class/ or bluprint.
Usually, people called create class as class definiton. Inside the class, there are data attributes and procedures.
The syntax of class definition is:
class MyClass: statement or instruction
That is formula to crate class. Example of simple class is :
class MyClass: x = 5 def func(self): print (hello)
That example is a class that have a attribute x = 5 and also method or function. The function is func().
Parameter and argument
Class have (1) data or property /attribute and also (2) methods or function. Function have characteristics in python. It have parentheses (). Inside parentheses, there is parameters. Parameter are specified value after the function name. Parameter in inside the parentheses. We can give empty parameter, one, two and more. We can add as many parameters as you want. To create multiple parameter we can just separate them with a comma
Value of parameter is argument. If your parameter is “name”, the argument is “John”, “Mary” and so on.
We can also give default value parameter. For example,
def my_function(country= "United States"): print("I am from " + country) my_function("Indonesia") my_function("Australia") my_function("Italia") my_function()
If the argument of function is empty, It will show default argument or parameter value.
Create an instance
To reuse a class, we can create instance of class. To reuse a class, we can import the class first, if the class in different file. If a class in same file with an instance, import is not necessary.
import MyClass newinstance = classname.function(parameter) newinstance.function(parameter)
Each instance of a class has characteristics of class. Each instance have clone datas and methods of class. Instance also can modify data without destroy class. So, class can be starting point to create new instance.