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

Daily News

GitHub’s new feature “passwordless authentication” aims to increase account security and provide a smoother user experience. By opting for the “Enable passkeys” option within...

Artificial Intelligence

Scientists from Japan used AI deep learning to discover new geoglyphs in the Arid Peruvian coastal plain, in the northern part of Peru’s Nazca...

Artificial Intelligence

The Tokyo Institute of Technology and other organizations have announced the start of development of highly performing generative artificial intelligence using the Fugaku supercomputer,...

Artificial Intelligence

Japanese researchers announced that AI ChatGPT technology has succeeded in the annual national doctor’s exam. The AI chatbot, which has advanced speech features that...