Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python class method - Is there a way to make the calls shorter?

I am playing around with Python, and I've created a class in a different package from the one calling it. In this class, I've added a class method which is being called from my main function. Again, they are in separate packages. The line to call the class method is much longer than I thought it would be from the examples I've seen in other places. These examples tend to call class methods from within the same package - thus shortening the calling syntax.

Here's an example that I hope helps:

In a 'config' package:

class TestClass :
   memberdict = { }

   @classmethod
   def add_key( clazz, key, value ) :
      memberdict[ key ] = value

Now in a different package named 'test':

import sys
import config.TestClass

def main() :
   config.TestClass.TestClass.add_key( "mykey", "newvalue" )
   return 0

if __name__ == "__main__" :
    sys.exit( main() )

You can see how 'config.TestClass.TestClass.add_key' is much more verbose than normal class method calls. Is there a way to make it shorter? Maybe 'TestClass.add_key'? Am I defining something in a strange way (Case of the class matching the python file name?)

like image 780
Kieveli Avatar asked Dec 14 '22 03:12

Kieveli


1 Answers

from config.TestClass import TestClass
TestClass.add_key( "mykey", "newvalue" )
like image 158
John Millikin Avatar answered Dec 28 '22 22:12

John Millikin