Python in Classes and Objects

This means that Virtually all the code is implemented using a special construct called courses. Programmers use courses to keep related matters together. This can be done using the keyword”class,” which is a grouping of object-oriented constructs.

From the end of this tutorial you will be able to:

  1. Define what is a class
  2. Describe how to create a class
  3. Define what is a method
  4. Describe how to do object instantiation
  5. Describe how to create instance attributes in Python

What is a class?

Objects have member Factors and have behaviour related to them. In python a class is created by the keyword class.

An item is created using the constructor of the class. This item is then going to be known as the instance of the course. In Python we create cases in the following manner

Instance = class(arguments)

How to create a class

The easiest class can be made using the class keyword. For example, Let’s create a simple, empty class without a functionalities.

>>> class Snake:
...     pass
... 
>>> snake = Snake()
>>> print(snake)
<__main__.Snake object at 0x7f315c573550>

Attributes and Methods in class:

A class by itself is of no use unless there is some functionality associated with it. Functionalities are defined by setting attributes, which act as containers for data and functions related to those attributes. Those functions are called methods.

Attributes:

You are able to specify the next class with the title Snake. This course will get an attribute name.

>>> class Snake:
...     name = "python" # set an attribute `name` of the class
...

You can assign the class to a variable. This is known as object instantiation. 
You will then have the ability to access the attributes that are found within the course using the scatter . 
operator. By way of example, from the Snake example, you can get the characteristic title of this category Snake.

>> # instantiate the class Snake and assign it to variable snake
>>> snake = Snake()

>>> # access the class attribute name inside the class Snake.
>>> print(snake.name)
python

Methods

Functions which will get into the class feature. These functions are known as methods. When you specify methods, you’ll have to always offer the initial argument to the procedure with a self keyword.

By way of instance, you can specify a course Snake, that has one feature title and yet another method change_name. The method alter title will take in a debate new_name together with the key word self.

At this time you may instantiate this course Snake using a changeable snake then change the title together with the procedure change_name.

>>> class Snake:
...     name = "python"
...     
...     def change_name(self, new_name): # note that the first argument is self
...         self.name = new_name # access the class attribute with the self keyword
...

>>> class Snake:
...     name = "python"
...     
...     def change_name(self, new_name): # note that the first argument is self
...         self.name = new_name # access the class attribute with the self keyword
...

At this time you may instantiate this course Snake using a changeable snake then change the title together with the procedure change_name.

>>> # instantiate the class
>>> snake = Snake()

>>> # print the current object name 
>>> print(snake.name)
python

>>> # change the name using the change_name method
>>> snake.change_name("anaconda")
>>> print(snake.name)
anaconda

Instance Features in python along with the init method

You might even offer the values to the features at runtime. This is accomplished by specifying the features within the init procedure. This example illustrates this.

class Snake:

    def __init__(self, name):
        self.name = name

    def change_name(self, new_name):
        self.name = new_name

Now you can directly define separate attribute values for separate objects. For example,

>>> # two variables are instantiated
>>> python = Snake("python")
>>> anaconda = Snake("anaconda")

>>> # print the names of the two variables
>>> print(python.name)
python
>>> print(anaconda.name)

change_name.

>>> # instantiate the class
>>> snake = Snake()

>>> # print the current object name 
>>> print(snake.name)
python

>>> # change the name using the change_name method
>>> snake.change_name("anaconda")
>>> print(snake.name)
anaconda

Instance Features in python and the init method

You can also provide the values to the attributes at runtime. This is done by defining the features within the init procedure. The following example illustrates this.

class Snake:

    def __init__(self, name):
        self.name = name

    def change_name(self, new_name):
        self.name = new_name

Now you can directly define separate attribute values for separate objects. For example,

>>> # two variables are instantiated
>>> python = Snake("python")
>>> anaconda = Snake("anaconda")

>>> # print the names of the two variables
>>> print(python.name)
python
>>> print(anaconda.name)