Connect with us

Hi, what are you looking for?

Class And Method In Python
Class And Method In Python

Programming

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

clean code is essential for updating scripts, adding new features to code, etc. So every developer, must learn how to clean the code. With...

Daily News

Cloud computing is a system that is often heard today and that most developers use at least once. In this article, we’ll look at...

Game Development

Visual Studio Code without a doubt, most popular and loved code editor in the code editor market. Many developers love working with Vs Code....

Daily News

GitHub Copilot was introduced last year and continues to make it easier for developers with nice updates to date. Although most people see Github...