Repository
What Will I Learn?
- Class and Object
- Init method and Access function in a class
Requirements
- Basic Python
- Install Python 3
- Install Flask
Resources
- Python - https://www.python.org/
- Flask - http://flask.pocoo.org/
- Jinja2 -http://jinja.pocoo.org/docs/2.10/
- Bootstrap -https://getbootstrap.com/docs/3.3/getting-started/
Difficulty
Basic
Tutorial Content
hi all, my tutorial this time is still discussing the python programming language. This python tutorial will discuss the concept of OOP in Python. In this tutorial still learning the Python programming language. This python tutorial will discuss the concept of OOP in Python. For those of you who are familiar with the OOP concept in other programming languages, you'll definitely be easy to understand the concept of OOP in python. But for those of you who are new to the of programming, welcome to join we will start from the beginning. There are two concepts/methods that I know in Procedural and Object-oriented programming (OOP) programming. Procedurally can be seen from how to execute programming from row to row of the entire code but if we use the OOP method we will use a blueprint. what is meant by the blueprint ?. by making a blueprint we can create a class consisting of several functions, variables, and algorithms in it. which means we can run it according to our needs. let's start this tutorial.
Class and Object
We will start learning Class and Object, I will discuss in more detail from the other tutorials. Class is the most important part of the OOP concept, It is not possible to use the OOP method without a Class. The Class is a blueprint that we will use to make the code more efficient and more structured. You can create a class in python like the code below:
oop.py
# Define Class
class Team():
teamName = 'AC Milan'
def getTeam(self):
return self.teamName
## Outside Class
team = Team()
print(team)
I created a oop.py file whose contents will be a blueprint of our code.
Note: it would be better if the file name matches the contents of the class in the file because it will make it easier for us to create an abstraction from a class itself.We can take classes this way
class Team()
, It would be better if the function name is uppercase.Unlike javascript and php programming, The python class has no opening and closing limits. Class in python uses a code block or row of tabs to indicate a part of the class. we can see in the picture below:
You can define variables and functions in the code block of the class as above.
- Function in class: we can make functions or methods in the class in this way
def getTeam(self):
self is a mandatory parameter that we use to access properties that exist in that class, If we look at javascript this is likethis
.
Note: Every function in the class must have a mandatory parameter, namely self.
- Use a class: To use a class we must make an abstraction from the class or copy of that class. such as the following
team = Team()
and to see what the results of our abstraction are foam doprint(team)
.
Now the team variable has access to the object class that has been defined. Example print(team.teamName)
.
- Access functions in class
Besides accessing variables we can also access functions in the class in this way:
oop.py
# Define Class
class Team():
teamName = 'Arsenal'
def getTeam(self):
return self.teamName
## Outside Class
team = Team()
print(team.getTeam())
We can access functions in the class in this way team.getTeam()
.
- Inject function parameters in class
Of course in its actual function, we will not only run the function, as usual, usually, when running the function we will pass some of the parameters of the function, but we can also see the example below:
## PYTON
# Define Class
class Team():
teamName = 'Arsenal'
def getTeam(self, team):
self.teamName = team
return self.teamName
## Outside Class
team = Team()
print(team.getTeam('Liverpool')) //Passing a parameter
- Can be seen in the picture above I pass a parameter in the function
team.getTeam('Liverpool')
and then we accept these parameters in thedef getTeam (self, teamName):
, we have defined a variable which isteamName = 'Arsenal'
, We will replace this variable with the value that we pass togetName()
. to access variables that in our class use the self keyword. Well, we can replace the variable like thisself.teamName = team
the team is the variables that have been passed to the function. We can see the results like the following:
Init method
The init method is a class that is automatically called when we create objects from the class we use. The name of this class should not be creative, we must define the class as __init__
. For more details, we can see the example below
oop.py
class Team():
teamName = ''
player = ''
def __init__(self, team, player):
self.teamName = team
self.player = player
def getTeam(self):
return self.teamName
def getPlayer(self):
return self.player
Like other functions, the init method also has a mandatory parameter, namely self. When creating objects from a class, we can pass additional parameters to the class object, these parameters will automatically be passed and accessed by the init method.
We can access properties inside classes such as variables and functions, For the example above, we will access the variable and give a value with the parameters we get from the object that we created
self.teamName = team
. Now the value of the variable teamName depends on the value passed when creating the object. So the variable has value when we access the class. We can see the example in thegetTeam()
function I will return the value of the variable teamName
def getTeam(self):
return self.teamName
- Access the init method
We have made the init method, now we will use the method. We can see in the code below:
oop.py
team = Team('Liverpool', 'Roberto Firmino')
print(team.getPlayer() + ' is the player from ' + team.getTeam())
When creating objects from the Team class () we pass two parameters, namely ('Liverpool')
and 'Roberto Firmino'
. These parameters will enter the init method and will be the parameters of the team and player.
def __init__(self, team, player):
self.teamName = team
self.player = player
- And we can set the value of variable teamName
self.teamName = team
and playerself.player = player
. Now we can see the results as shown below.
We have learned about classes and objects in python Object-oriented programming (OOP) then we learn how to concept from the init method. there are still many things we must learn in the OOP concept. I hope this becomes your basis to master or learn more about Python because there will be a lot of benefits that we will get if we are able to master OOP in Python. thank you for following this tutorial. may be useful.
Curriculum
- Web development with flask
Web developement with python #1 : Flask initialization and Routing system
Web development with python #2 : Templating jinja2 and Method POST on routing system
Web development with python #3 : Get method, Query parameter and Navigate Routing
Web development with python #4: Store cookie and Get cookie in template
Web development with python #6 : Use flash message and combine it with framework boostrap
- File in python
File in python #1 : Read file and Write file and Modes file
File in python #2 : The interaction between user input, Read CSV file
File in python #3 : Write CSV file, Read and Write JSON file
- Class-based views
Tutorial Django - Class based view #2 : Use Class based view method and Get and Post method
Thank you for your contribution.
Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.
To view those questions and the relevant answers related to your post, click here.
Need help? Chat with us on Discord.
[utopian-moderator]
thank you for the review @mcfarhat, I aimed this tutorial at the most basic stage.
Thank you for your review, @mcfarhat! Keep up the good work!
You made a good overview of the all possible cases if init class fields
thanks dude @pinkwonder. but there is still much better documentation than I made
Congratulations @duski.harahap! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :
Click here to view your Board
If you no longer want to receive notifications, reply to this comment with the word
STOP
To support your work, I also upvoted your post!
Do not miss the last post from @steemitboard: