Python OOP #1 : Class and Object, Init method

in #utopian-io5 years ago (edited)

Repository

https://github.com/python

What Will I Learn?

  • Class and Object
  • Init method and Access function in a class

Requirements

  • Basic Python
  • Install Python 3
  • Install Flask

Resources

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:

Screenshot_1.png

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 like this.

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 do print(team).

Screenshot_2.png

Now the team variable has access to the object class that has been defined. Example print(team.teamName).

Screenshot_3.png

  • 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())

ezgif.com-video-to-gif.gif

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

Screenshot_4.png

  • Can be seen in the picture above I pass a parameter in the function team.getTeam('Liverpool') and then we accept these parameters in the def getTeam (self, teamName):, we have defined a variable which is teamName = 'Arsenal', We will replace this variable with the value that we pass to getName(). to access variables that in our class use the self keyword. Well, we can replace the variable like this self.teamName = team the team is the variables that have been passed to the function. We can see the results like the following:

ezgif.com-video-to-gif (1).gif

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 the getTeam() 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 player self.player = player. Now we can see the results as shown below.

Screenshot_5.png

ezgif.com-video-to-gif (3).gif

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 #5: Web development with python #5 : Session in flask and Login and Logout system

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 views #1 : Installation and configuration Django, Using a template system

Tutorial Django - Class based view #2 : Use Class based view method and Get and Post method

Tutorial Django- Class based view #3 : Authentication in Django and Urls Protection globally and specifically

Proof of work done

https://github.com/milleaduski/python-web-app

Sort:  

Thank you for your contribution.

  • The language, description, and approach in the tutorial is of very low quality, and of repetitive nature. At times the content is barely readable or understandable.
  • OOP concept for Python is already well covered across the web.

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) :

You published more than 60 posts. Your next target is to reach 70 posts.

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:

SteemWhales has officially moved to SteemitBoard Ranking
SteemitBoard - Witness Update

Support SteemitBoard's project! Vote for its witness and get one more award!

Coin Marketplace

STEEM 0.31
TRX 0.12
JST 0.034
BTC 64418.55
ETH 3157.64
USDT 1.00
SBD 4.06