Python. Classes and modules. Referring to the classes of other modules




Classes and modules. Referring to the classes of other modules


Contents


Search other resources:

1. Referring to the class from another module. Method 1

A class can be declared in an interactive shell or a module. If a class is declared in the module, it is considered an attribute of the module. This means that the class name can be accessed using the following form

moduleName.ClassName

here

  • moduleName – the name of the module whose class you want to include to the current module;
  • ClassName – имя класса в модуле moduleName.

For this method to work, you first need to execute the import directive

import moduleName

This way of referring to a class is also used in the case of inheriting this class.

Example. Figure 1 shows the call to the Show() method of the Hello class of Module1 from the code of Module2.

Python. Referring to the class from another module

Figure 1. Referring to the Hello class from another module

After running the “Module2.py” program, the following result will be displayed

Module1.Hello.Show

 

2. Referring to a class from another module. Method 2

With this method, the class of another module can be accessed without using the module name in front of the class name (see the previous method). In this case, you need to use a combination of from and import directives according to the following pattern

from moduleName import ClassName

here

  • moduleName is the name of the module in which the ClassName class is declared;
  • ClassName is the name of the class that is declared in the module moduleName.

After this connection, you can refer to the ClassName without specifying the module name in front of it.

Example. Figure 2 shows an example of accessing the Triangle class without using the name Module1.

Python. A way to access a class name using a combination of from-import directives

Figure 2. A way to access a class name using a combination of from-import directives

 

3. The case when class and module have the same name

There are times when a module and a class in it have the same name. Then, in order to access the class name using method 1 (see p. 1), you must prefix it with the module name.

If you do not specify a module name, an exception will be thrown. A single name is interpreted by the interpreter as a module name, not a class name. The following example explains this point.

Example. The circle.py module is defined, which implements the Circle class

# Module circle.py
# The class that defines the circle

import math

class Circle:
    # Initial initialization method
    def __init__(sl, x, y, r):
        sl.x = x
        sl.y = y
        sl.r = r

    # Returns the area of a circle
    def Area(sl):
        return math.pi*sl.r*sl.r

Then, in order to use the Circle class in another module, you need to specify the module name when referring to its name.

# A module named module2.py
# Including the circle module

import circle

# Referring to the Circle class
# be sure to specify the module name
obj = circle.Circle(2, 3, 5)
area = obj.Area()
print("area = ", area)

The result of the program

area = 78.53981633974483

If in the line

obj = circle.Circle (2, 3, 5)

remove the name of the module circle

obj = Circle (2, 3, 5)

then an exception will be thrown.

 

4. The case when modules have classes with the same names

If a class is declared in a including module whose name coincides with the name of the class of the current module, then the following rule applies:

  • to access the class of another module, be sure to set the name of the other module before the class name. In this case, the method using from-import directives does not work. Only the way using the import directive works.

Example. In the example, classes with the same names are declared in different modules. For such a case, the correct way to use the class of another module is demonstrated.

The text of module1.py module.

# A module named module1.py
# The module declares a class named A

class A:
    # Some class method
    def A(self):
        print("Module1: A.A()")

The text of module2.py module. This module shows how to access the A class of module1.py.

# The module2.py module
# Include module1.py.
# Only in this way, the combination of directives from-import does not work
import module1

# This module has its own class named A
class A:
    def A(self):
        print("Module2: A.A()")

# 1. This module has its own class named A
obj = A() # which class will be chosen???

# The class of the current module is selected
obj.A() # Module2: A.A()

# 2. If you need to refer to the class A of module1,
#    then this is the only way
obj2 = module1.A()
obj2.A()

If instead of the line

import module1

you type

from module1 import A

then in the line

obj2 = module1.A()

will give an error

NameError: name 'module1' is not defined

After starting the program will give the following result

Module2: A.A()
Module1: A.A()

 

5. Common conventions for naming modules and classes

When writing large projects containing a large number of modules and classes, correct identification is important. Therefore, Python has a consistent convention for class and module names:

  • it is customary to start module names with small letters;
  • class names start with capital letters.

This is to ensure better visual differences.

For example. The graphics module implements the DrawLine class. Then referring to the class name using the module name will be as follows

import graphics

...

graphics.DrawLine

 


Related topics