Okay python gurus, this one has me stumped. Is it possible to introspect and obtain the instance of a class that assigned a class?

I have a simple sample that demonstrates what I'm looking for, but doesn't make any sense why you would do it.

class One(object):
    def print_parent_hw(self):
        ?????????
        ?????????.print_hw()

class Two(object):
    def __init__(self):
        self.cls = One()
    def print_hw(self):
        print "Hello World from One"
    def have_one_print_two(self):
        self.cls.printparent_hw()

a = Two()
a.have_one_print_two()

I could do it by rewriting some code, but this situation could be cleaner and quicker (even though the sample doesn't make much logical sense). I realize I could do it like this, but it doesn't seem right, and you end up with a circular attribute (i.e. a.cls.parent.cls.parent.cls.parent.cls.......)

class One(object):
    def __init__(self, parent):
        self.parent = parent
    def print_parent_hw(self):
        self.parent.print_hw()

class Two(object):
    def __init__(self):
        self.cls = One(self)
    def print_hw(self):
        print "Hello World from One"
    def have_one_print_two(self):
        self.cls.print_parent_hw()

a = Two()
a.have_one_print_two()

Recommended Answers

All 4 Replies

Let class Two inherit class One:

class One(object):
    def print_parent_hw(self):
        self.print_hw()

class Two(One):
    """class Two inherits class One"""
    def __init__(self):
        # make instance of One part of self
        One.__init__(self)
    def print_hw(self):
        print "Hello World from One"
    def have_one_print_two(self):
        self.print_parent_hw()

a = Two()
a.have_one_print_two()  # Hello World from One

Perhaps I should have been more specific in my example. I'm using paramiko to establish an ssh session, and the newly called class has methods to act on the session, but needs access to the parent instance methods (i.e. ssh_exec)

Here is a simplified example. In the real world script, the One class would be loaded using __import__ based on it subclassing a base class called plugin then looking at Plugin.__subclasses__(). Which is based on the examples at this link http://lucumr.pocoo.org/2006/7/3/python-plugin-system

import paramiko
class One(Plugin):
     def shutdown(self):
         command = 'shutdown'
         ????????
         ????????.ssh_exec(command)

class Two(paramiko.SSHClient):
    def __init__(self, <options>)
        self.connect(<options>)
        cls = One()
        cls.shutdown()

Please do not turn your question into a moving target!
It is very frustrating to the people who are trying to help you.
Try to state your question in it's full scope the first time around.

I didn't move my target, and the original question was in full scope. While I appreciate your help, your answer didn't address my question. While my original sample could have been more on target, at what point do you draw the line, short of posting 100's of lines of code?

However, I do believe I've found my solution. I am passing the self.exec_command() function in as a parameter to the assigned class. This allows me to also pass a debug function instead, which just prints commands vs. executing them.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.