0

Suppose I will send self.f to a global method,

def write_to_report(file_hdlr, content):

If I want get the what the object is the file_hdlr belonged to.

How to get it ? Because I need to know the file_hanlder is belonging which Class, and use the class name to do somthing

def write_to_report(file_hdlr, content):
    file_hdlr.__self__ # ??? NO

CODE

class MemoryInfo(threading.Thread):        
    def __init__(self, config):
        self.f = get_export_file_hdlr(self.__class__.__name__)

class CPUInfo(threading.Thread):        
    def __init__(self, config):
        self.f = get_export_file_hdlr(self.__class__.__name__)
1
  • 2
    Objects do not have an implicit pointer to their "parent". If you think about it, the concept of "parent" does not even make sense, because when a reference is passed, a copy of the reference is made, and this copy is just as valid as the original reference. Now your object belongs to two different objects. Commented Jun 18, 2014 at 7:08

2 Answers 2

1

You can't.

A different approach:

class ReportWriterMixin(object):
    def write_to_report(self):
        this_class = self.__class__
        # do thing with self.f based on this_class

class CPUInfo(threading.Thread, ReportWriterMixin):        
    def __init__(self, config):
        self.f = get_export_file_hdlr(self.__class__.__name__)
        self.write_to_report()

This way, while your method is no longer "global", it can be made available to any class you like

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

Comments

0

If you want to know the type of file_hdlr variable, you can use function type:

file_hdlr_type = type(file_hdlr)

2 Comments

He's asking how he can get the original self reference that contained file_hdlr....
Sorry, I didn't understand

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.