Inheritance and Import

This is my first question here on stackoverflow, so if I make one or two mistakes, please allow me now. In addition, I am not a native speaker of English, so be prepared for grammar errors.

The thing is, I am a complete beginner, and I am learning programming through python and javascript. Right now, I have covered the main issues, and I'm experimenting with my first python application, so this is the first time I train with modules, inheritance, etc. And I have some doubts regarding importing methods / classes through various modules

  • I am writing a class (e.g. Class_1). Then I write another class (e.g. Class_2) in another module. Class_2inherited from Class_1. At the same time, I have some methods from another module imported at the method level Class_1.__init__. When I write Class_2, do I need to import the method again, __init__methods imported into Class_1, or does inheritance also mean methods imported at the level Class_1?

Example:

class Class_1(object):
    def __init__(self, args):
    from Module_5.Module_A import method_x
    # Class definition...

class Class_2(Class_1):
    def __init__(self, args):
    from Module_5.Module_A import method_x again?
    # Class definition...
  1. , , 10 30 . , (, method_x) ( , __init__ ). , method_x, , ? "" " " ( ), , ) b) 1 < [ , ] < [ ] ?
+4
2

. ,

from Module_5.Module_A import method_x

class Class_1(object):
    # Class definition...

class Class_2(Class_1):
    # Class definition...

.

+4

Gribouillis, import .

import , , , . (, , , , , , , ).

- , , script.

if __name__ == '__main__':

, , script. (, argparse), . , .


import Module_5.Module_A

from Module_5.Module_A import method_x

Module_5.Module_A , , Module_5.Module_A , method_x . from , , , , , . -

import Module_5.Module_A as m5A

:

m5A.method_x

, method_x m5A.


: " Class_2, , Class_1, , Class_1 level?". , , . .

, 3 .

m1.py

def f1(x):
    return x

m2.py

import m1

def f2(x):
    return m1.f1(x) * 2

m3.py

import m2

def f3(x):
    return m2.f2(x) * 3

print(f3(1))

, m3.py m1.py, m1.f1, m2.f2 m1.f1 - import m2.py.

0

Source: https://habr.com/ru/post/1683576/


All Articles