Connect with us

Hi, what are you looking for?

Programming

Class And Method In Python

Class And Method In Python
Class And Method In Python

In this python tutorial, we going to look at the class structure before start reading this article, if you don’t know functions you should read this article. This tutorial consist of class and method in python.

Class Syntax

We store the same type of functions in classes, and we can call the function from that class whenever needed. Classes consist of a combination of objects of the same type.

class className:
   functionName
   operation
An Example Class

Let’s create a sample class and call this class in the project. In this example class, let’s print a value on the screen when it is called, then we will enrich these examples with a different function.

class Product:
   productName = "Phone"

product = Product()
print(product.productName)

In the above example, we call the name object from the Product class and write to the screen. Let’s look at a new function structure. This function structure is init functions.

Init Functions

You can customize your classes with init, the init functions are run every time the class is called. The parameters in the init function must be added when the class is called.

class Product:
   def __init__(self, name, id):
      self.name = name
      self.id = id
   
   def GetCode(self):
      return self.name + self.id

product = Product("Phone" , "1")
print(product.GetCode())

The self paremeter is also used to use the arguments we have in other functions. In this function, the name of the product and its unique number are written and we create a code with the create code function.

Accessing variables in classes

By accessing the data in your class, you can give the user the ability to modify the data or delete an object. Let’s look at these features in an example class.

class Product:
   def __init__(self, name, id):
      self.name = name
      self.id = id
   def GetCode(self):
      return self.name + self.id

product = Product("Phone" , "1")
product.name = "Iphone"
product.id = 2
print(product.GetCode())

You can change the arguments we first assigned by accessing those data with the above method. Now let’s delete the objects.

class Product:
   def __init__(self, name, id):
      self.name = name
      self.id = id

product = Product("Phone" , "1")
del product.name
print(product.name)

Here you will get an error message. The reason for this error message is that the program can no longer find the name object because we deleted it with the del command of the name object.

CONGRATULATIONS, YOU FINISHED CLASS IN PYTHON!

Click to comment

Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like

Programming

This article was created for those who want to use functional programming in their career or who are learning new programming paradigms. We will...

Programming

Functions are pieces of code that combine the integrity of the code used to perform a task into a single structure. If you are...

Programming

Computers can solve an event in different ways, we call these ways algorithms, each algorithm works differently from the others and has different processing...

Data Science

SQL (Structured Query Language) is one of the most used tools by data scientists, data analysts, and data engineers. Almost all companies use a...