Separating a class definition and its implementation in python

I am starting Python, and the main language is C ++. You know that in C ++ very often the definition and implementation of a class is separated. (How) Does Python do this? If not, how to get a clean class interface profile?

+6
source share
3 answers

There is no such concept in Python. If I understand your needs correctly, a "clean profile" should be generated by proper class documentation.

You can also use the introspection capabilities of Python to programmatically access all methods of the class .

+6
source

Python programming is in many ways different from C ++. If you want to know how to write quality, professional level code in python, then this is a good article to get you started. Good luck.

+1
source

For some reason, many Python programmers combine a class and its implementation into the same file; I like to separate them, if absolutely necessary.

It's simple. Just create an implementation file, import the module in which this class is defined, and you can call it directly.

So, if the ShowMeTheMoney class is defined inside class1_file.py , and the file structure is:

  /project /classes /__init__.py /class1_file.py /class2_file.py /class1_imp_.py 

(BTW, the file and class names must be different: the program will fail if the class and file names are the same.)
You can implement it in class1_imp_.py using:

 # class1_imp_.py import classes.class1_file as any_name class1_obj = any_name.ShowMeTheMoney() #continue the remaining processes
# class1_imp_.py import classes.class1_file as any_name class1_obj = any_name.ShowMeTheMoney() #continue the remaining processes 

Hope this helps.

+1
source

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


All Articles