Python instantiate class in class

Inner Class in Python

Python is an Object-Oriented Programming Language, everything in python is related to objects, methods, and properties. A class is a user-defined blueprint or a prototype, which we can use to create the objects of a class. The class is defined by using the class keyword.

Example of class

Python3

First of all, we have to understand the __init__() built-in method for understanding the meaning of classes. Whenever the class is being initiated, a method namely __init__() is always executed. An __init__() method is used to assign the values to object properties or to perform the other method that is required to complete when the object is created.

Example: class with __init__() method

Python3

Course : Campus Preparation Duration : As per your schedule

Inner Class in Python

A class defined in another class is known as an inner class or nested class. If an object is created using child class means inner class then the object can also be used by parent class or root class. A parent class can have one or more inner classes but generally inner classes are avoided.

We can make our code even more object-oriented by using an inner class. A single object of the class can hold multiple sub-objects. We can use multiple sub-objects to give a good structure to our program.

  • First, we create a class and then the constructor of the class.
  • After creating a class, we will create another class within that class, the class inside another class will be called an inner class.
Читайте также:  Метод merge map java

Python3

Name: Green Name: Light Green Code: 024avc

Why inner class?

For the grouping of two or more classes. Suppose we have two classes remote and battery. Every remote needs a battery but a battery without a remote won’t be used. So, we make the Battery an inner class to the Remote. It helps us to save code. With the help of the inner class or nested class, we can hide the inner class from the outside world. Hence, Hiding the code is another good feature of the inner class. By using the inner class, we can easily understand the classes because the classes are closely related. We do not need to search for classes in the whole code, they all are almost together. Though inner or nested classes are not used widely in Python it will be a better feature to implement code because it is straightforward to organize when we use inner class or nested class.

# create NameOfOuterClass class class NameOfOuterClass: # Constructor method of outer class def __init__(self): self.NameOfVariable = Value # create Inner class object self.NameOfInnerClassObject = self.NameOfInnerClass() # create a NameOfInnerClass class class NameOfInnerClass: # Constructor method of inner class def __init__(self): self.NameOfVariable = Value # create object of outer class outer = NameOfOuterClass()

Types of inner classes are as follows:

Multiple inner class

The class contains one or more inner classes known as multiple inner classes. We can have multiple inner class in a class, it is easy to implement multiple inner classes.

Example: Multiple inner class

Источник

Python Instantiate Class Creating and Instantiating a simple class in python

Last month i started taking a course on python and i stumbled on a tutorial on object oriented programming from the python perspective, and through out this week and coming weeks i would be sharing what i have been able to learn. Today we would be looking at class creation and instantiation. To help us understand this better we would be creating an employee class, each employee would have attributes like name,email,pay,etc. Now lets have a head first attempt at creating and instantiating a class. Alt text of image The above code snippet shows how to create a class in python, the pass keyword under tells python to neglect the class, without this keyword, python would see the class as an empty one and return an error. A class is a blue print for creating instances and each unique employee that we create would be an instance of that class. Example:
Alt text of image They above code shows the creation of unique instances of the Employee class and to assign data to this instances we would be using instance variables. Instance variables contain data that is unique to the instance alone. To create instance variables for each employee we would do the following: Alt text of image Imagine a situation we have 1000 employees to create, trying to set the variables each time we want to create an employee would mean having a-lot of code and errors. But python allows you to be able to create instances automatically and this we could do using its (init) method. Alt text of image the init method after creation receives an instance automatically called (self), and we also pass in other attributes of the Employee class like name and pay. Next stop we would be looking at setting the instance. Alt text of image
with that done, when we create our employee, the instance would be passed in automatically, all we would bother ourselves with would be to provide the employee attributes.
Alt text of image
on running this script (emp_1) would be passed into the (init) method as self.
Now suppose we want to print each users fullname, we could actually use the format function in python to concatenate strings to get the fullname, but this would require doing this every time we want to print an employees fullname, but to better enjoy the advantage of code re-use, we would be creating a method to help us generate each employees fullname.
Alt fullname That is it for class and instance of a class creation. Below is the full source code for the piece.
Alt text of image (Note)
while trying to print employees fullname, you could either do this—> (emp_1.fullname()) or you could try (Employee.fullname(emp_1)), they both mean the-same thing, in-fact the later is what happens in the background when you run the program. Tomorrow we would be looking at the difference between instance variables and class variables, the difference between regular,static and instance methods

Читайте также:  Css input text title

Top comments (9)

Sr. Software Engineer & Tech Lead. 10+ years of experience, primarily at Amazon.com. Currently at Relief.app My opinions are mine.

Nice. Some constructive criticism:

Try using the markdown blocks for code:

Makes for a more readable page and saves you time (copy and paste code instead of taking and uploading screenshots) and allows readers to copy code directly.

9 likes Like Comment button

Источник

Оцените статью