Class definition in Python

Class definition in Python

The class in Python is a continuation and complement of object orientation, which we talked about in the article about the basic concepts of introductory


If you have any questions, ask!

The class in Python is a continuation and complement of object orientation, which we talked about in the article about the basic concepts of introductory Python programming .

We want to discuss the complete definition of a class in Python, so stay with Iteros Academy.

Class definition in Python

You can create different values ​​in the class and use them in the program.

class MyClass:
  x = 5

The simplest type of class has been defined above, which I will complete over time.

We start the class with the letter class and then define a name for it and finally two hour points

As you can see in the example, we have a variable in the body of the class, which we have given the value of five.

We can use the above class in the program but let's extend our class.

So that this conversation is not dumb for you later, we create an object at the very beginning.

 

Object definition

You can define objects for the created class. For example, suppose you have a folder where you have defined your classes.

Now you want to use that class in different parts. For this, it is enough to create an object class from it.

p1 = MyClass()
print(p1.x)

Above the class MyClass is created. Suppose this class is written in the folder where the project classes are located.

Now with the written code you can use the properties of that class. p1 is an object created from class MyClass that represents the value of x. Basically, x is a property of the MyClass class that is used in the p1 object.

Maybe the desired class has more than one feature, but you want it to use one of them in your object.

So far, the general structure of the class has been defined, in the following we want to expand it.

 

Constructors in Python

In Python, class functions are marked with (__) because they have a special meaning.

Continue __init__

One of the most popular and widely used functions is the __init__ function. This is the constructor function.

When you create an object of a class, you can define the initial arguments with the __init__ function.

  
class Person:

  inname = 'John'
    vague = 20
	
  def __init__(self, name, age):
    self.name = name
    self.age = age

p1 = Person("John", 36)

print(p1.name)
print(p1.age)

Pay attention to the above class. Through this function, we defined the primary arguments of name and age.

But there is one point: vname and vage are class variables, but name and age are object variables.

When creating any object from the Person class, the name attribute must be assigned to the equivalent value of the name input parameter.

Also, the command self.age = age assigns the age attribute to the value of the age input parameter for any object created from this class. But what is self?

Other content from Python

What is self?

self causes the parameters of each object to be called. In the above example, the name variable is called by self.

  1. At first, we created name and age by __init__ function.
  2. We defined it by self.
  3. And finally we called it.

There is no need to use self when calling an object variable because Python will automatically identify it.

 

As you can see in the example above, you can define a function in the class.

You do not need to use the word self to call the parameters. But if you want to put an arbitrary name, you should note that the name must be the first parameter of each function of the class, as in the following example:

  
class Person:
  def __init__(mysillyobject, name, age):
    mysillyobject.name = name
    mysillyobject.age = age

  def myfunc(abc):
    print("Hello my name is " + abc.name)

p1 = Person("John", 36)
p1.myfunc()

Delete objects or Delete Objects

In the Python programming language, unused objects are deleted from memory to free resources.

If you want to delete an object, this action is done like the code below.

  
of p1

Additional items on class definition in Python

Class: A set of objects that have the same characteristics make up a class.

In other words, a dedicated or defined-user instance that allows the creation of objects or multiple instances of

gives a common pattern.

Each class is a separate unit of the application that includes data in the form of member variables called attributes and operations defined in the form of methods.

As mentioned, attributes are class members that hold data (class variable and instance variable) and methods are class behaviors and perform specific operations.

To access the methods and variables of the class member, it is enough to use the dot operator.

Class member variables: A variable that is shared between all created instances of a class is called a class member variable or class variable.

Member variables are defined inside the body of the class but outside the scope of the methods of that class.

Class variable or class member variables are used much less than instance variables created from class or instance variable.

Member Data: A class variable or instance variable that contains data related to a class and objects made from it.

overloading function: assigning and defining several behaviors for the same function.

In other words, creating multiple different versions of a function with the same name.

Now, the operation that the function performs depends on the type of objects or arguments that are involved.

variable instance: a variable that is defined inside the exclusive field or the body of the method and belongs only to the current instance of the class.

Inheritance: Transferring the characteristics of a class to the classes that are derived from it is called Inheritance.

instance: A single object of the class is called Instance.

For example, an object called obj that belongs to a class called Circle.

In fact, it is a created example of the Circle class, which is a template for creating objects.

Instantiation, instantiation of a class: Creating an object or an instance of the desired class is called instantiation.

method: the same function that is declared inside the body or the exclusive scope of the class.

Object: It is a unique instance that is created from the desired class.

In addition to member data, an object can contain class member variables and instance member variables created from the method class.

operator overloading: assigning multiple behaviors to the same operator.

More precisely, it is a special example of polymorphism where operators can have different implementations based on arguments and input parameters.

Source » Itroz Academy

Related articles


Array/List methods in Python
Functions in Python
Class definition in Python
Basic concepts of introductory Python programming

Comments (0)

You need to login to post a comment.