0

I have following class in Python:

class SDK(object):
    URL = 'http://example.com'

    def checkUrl(self, url):
        #some code

    class api:
        def innerMethod(self, url):
            data = self.checkUrl(url)
            #rest of code

but when I try to access checkUrl from api, I get error. I try to call nested method by:

sdk = SDK()
sdk.api.innerMethod('http://stackoverflow.com')

Is there any simple way to call inner class methods, or (if not) structurize methods into inner objects? Any suggestion will be appreciated.

Edit: class code:

class SDK(object):
    def run(self, method, *param):
        pass

    class api:
        def checkDomain(self, domain):
            json = self.run('check', domain)
            return json

run code:

sdk = SDK()
result = sdk.api().checkDomain('stackoverflow.com')
7
  • Show the rest of code Commented May 15, 2014 at 17:52
  • Whats the use of the inner class? Commented May 15, 2014 at 17:56
  • @Daniel I am trying to structurize code, thats why I wanted to use inner class. Do you have any better idea? Commented May 15, 2014 at 17:59
  • There doesn't seem to be any benefit to the api class over simply defining a method SDK.checkDomain. Commented May 15, 2014 at 18:00
  • I suggest you do this with modules, not inner classes. Commented May 15, 2014 at 18:01

2 Answers 2

2

The SDK class is not a parent of the api class in your example, i.e. api does not inherit from SDK, they are merely nested.

Therefore the self object in your api.innerMethod method is only an instance of the api class and doesn't provide access to methods of the SDK class.

I strongly recommend getting more knowledgeable about object-oriented programming concepts and grasp what the issue is here. It will help you tremendously.

As for using modules to achieve something along these lines, you can, for example, pull everything from the SDK class to sdk.py file, which would be the sdk module.

sdk.py:

URL = 'http://example.com'

def checkUrl(url):
    #some code

class api:
    def innerMethod(self, url):
        data = checkUrl(url)
        #rest of code

main.py:

import sdk

api = sdk.api()
api.innerMethod('http://stackoverflow.com')

Or you may go even further and transform sdk to a package with api being a module inside it.

See https://docs.python.org/2/tutorial/modules.html for details on how to use modules and packages.

Sign up to request clarification or add additional context in comments.

Comments

0

If you want a method to act as a classmethod, you have to tell python about it:

class SDK:
    class api:
         @classmethod
         def foo(cls):
                 return 1

Then you have access like

SDK.api.foo()

Depending on what you're trying to do, this smells kind of un-pythonic. If it's just the namespace you care about, you'd typically use a module.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.