Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Separating class definition and implementation in python

Tags:

python

I am a Python beginner and my main language is C++. You know in C++, it is very common to separate the definition and implementation of a class. (How) Does Python do that? If not, how to I get a clean profile of the interfaces of a class?

like image 610
updogliu Avatar asked Sep 22 '12 08:09

updogliu


3 Answers

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

You can also use Python's introspection capabilities to programatically access all the methods of a class.

like image 93
Yuval Adam Avatar answered Oct 13 '22 09:10

Yuval Adam


Python programming is different in many aspects from c++. If what you want to know is how to write quality, professional level code in python then this is a good article to start with. Good luck.

like image 1
Emil Avatar answered Oct 13 '22 11:10

Emil


    For some reason, many Python programmers combine the class and its implementation in the same file; I like to separate them, unless it is absolutely necessary to do so.
    That's easy. Just create the implementation file, import the module in which the class is defined, and you can call it directly.
    So, if the class - ShowMeTheMoney - 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 the file names are the same.)
    You can implement it in the class1_imp_.py using:

 
   # class1_imp_.py
   import classes.class1_file as any_name

   class1_obj = any_name.ShowMeTheMoney()
   #continue the remaining processes


 

Hope this helps.

like image 1
Kneel-Before-ZOD Avatar answered Oct 13 '22 11:10

Kneel-Before-ZOD