er1shivam
er1shivam

Reputation: 11

Python : How to keep definition and implementation of classes/methods separate in python ? See example use cases below

How can we define a class in one file and implement it in other in Python ? I know in C/C++ we can do this with header(.h) files.

For example..

#### Time .h

#ifndef TIME_H
#define TIME_H

class Time
{
     private :
          int hour;
          int minute;
          int second;
     public :
          //with default value
          Time(const int h = 0, const int m  = 0, const int s = 0);
          //    setter function
          void setTime(const int h, const int m, const int s);
          // Print a description of object in " hh:mm:ss"
          void print() const;
          //compare two time object
          bool equals(const Time&);
};
 
#endif

#include <iostream>
#include <iomanip>
#include "Time.h"
using namespace std;
 
Time :: Time(const int h, const int m, const int s) 
  : hour(h), minute (m), second(s)
{}
 
void Time :: setTime(const int h, const int m, const int s) 
{
     hour = h;
     minute = m;
     second = s;     
}       
 
void Time :: print() const
{
     cout << setw(2) << setfill('0') << hour << ":"
    << setw(2) << setfill('0') << minute << ":"
    << setw(2) << setfill('0') << second << "\n";   
 
}
 
bool Time :: equals(const Time &otherTime)
{
     if(hour == otherTime.hour 
          && minute == otherTime.minute 
          && second == otherTime.second)
          return true;
     else
          return false;
}

I want to know if something like this can be implemented in Python? Also I am trying to come up with a design to make it possible. I will appreciate any help on this. It is not mandatory to have the same syntax as mentioned below and I’m ready to do model it differently. Just that the definition should be separate than implementation. And so the client code can directly import required class or methods.

# species.py

Class Human:
    
    def __init__(self, a, b c, d):
        “””
        This initialises the Human class…..
        “””
    def __dothis__(self, g, h, i):
        “””
        Random documentation 
        “””

    def __getage__(self): 
        “””
        Random documentation 
        “””

# species_impl.py

def Human__init__(a, *args):
    print(“Arguments…”)
    print(a)
    
    for i in args:
        print(i)

def Human__dothis__(self, g, h, i):
    print(g, h, i)

def Human__getage__(self): 
    print(“hello this is your age”)

Upvotes: 0

Views: 233

Answers (1)

Barmar
Barmar

Reputation: 781096

You can do it by simply having each of the methods call the corresponding functions.

The class definition file can import the implementation file. Then the implementation names that are defined will only be visible to the definition file, not to the callers of the methods.

from species_impl import *

class Human:
    
    def __init__(self, a, b c, d):
        """
        This initialises the Human class…..
        """
        Human__init__(self, a, b, c, d)

    def __dothis__(self, g, h, i):
        """
        Random documentation 
        """
        return Human__dothis__(self, g, h, i)

    def __getage__(self): 
        """
        Random documentation 
        """
        return Human__getage__(self)

BTW, you probably shouldn't make up your own dunder methods, those should just be used for the built-in methods.

Upvotes: 1

Related Questions