views:

137

answers:

1

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?)

+12  A: 
from config.TestClass import TestClass
TestClass.add_key( "mykey", "newvalue" )
John Millikin
MUAH! Brilliant. Thanks.
Kieveli