Call Module in Other Class - python

So I have a close() method that is called when the user clicks the close button. The close method is as follows:
def close(self):
ThreadedClient.endApplication()
root.destroy()
The close() method is inside a GUI() class. The close method needs to call the endApplication() method in the ThreadedClient class. But instead it gives me this error:
TypeError: unbound method endApplication() must be called with ThreadedClient instance as first argument (got nothing instead)
This may be a simple solution, but I just don't know how to fix it. Any help is appreciated!

The question you need to be asking is which ThreadedClient needs to have .endApplication() called on it.
Once you have a reference to it (assuming you store a reference in self)
def close(self):
self.threaded_client.endApplication()
# ...

Apparently, endApplication() is an instance method, but ThreadedClient is a class. You need to provide it with the actual instance of ThreadedClient that you want to end.
For instance, if somewhere else you created a threaded client with...
foo = ThreadedClient()
then later on you'd need to call...
foo.endApplication()

Related

Calling a method of a class before creating an object

Let's say we have a class, and we are calling it's method before creating an object:
class ParentClass:
def MyMethod(self):
self.a=5
print("I am Method")
ParentClass.MyMethod(ParentClass)
Why do we get a result?
Also, why hasattr(ParentClass,'a') is showing that instance variable is created?
You get a result because you ParentClass.MyMethod evaluates to a regular function that takes one argument, which you supplied. The function doesn't care what the type of self is, as long as you can define an attribute named a for it. The result of ParentClass.MyMethod(ParentClass) is to add an attribute named a to ParentClass.
ParentClass().MyMethod, on the other hand, produces an instance of method that wraps the function, due to the descriptor protocol. The method, when called, simply calls the function with the instance as the first argument and the rest of its own arguments. Because the function doesn't expect any more arguments, you would get a TypeError.

In Python 2 do we need a self keyword for a function even if the function isn't part of a class?

So I'm looking through some old python 2 code and I see this function
def manage_addMapSamlPlugin(self, id, title='', delegate_path='', REQUEST=None):
""" Factory method to instantiate a MapSamlPlugin """
# Make sure we really are working in our container (the
# PluggableAuthService object)
self = self.this()
# Instantiate the adapter object
lmp = MapSamlPlugin(id, title=title, delegate_path=delegate_path )
self._setObject(id, lmp)
if REQUEST is not None:
REQUEST.RESPONSE.redirect('%s/manage_main' % self.absolute_url())
Now this function is outside of a class, the code compiles and doesn't give any errors. My understanding is that the self keyword in this case is just anything that gets passed in, but self.this() and self._setObject(id, lmp) that shouldn't be a thing right? Shouldn't the compiler throw an error? The code is run on a terminal in a ssh server I don't know what compiler it uses.
At the end of the file this is where the function gets called.
def initialize(context):
registerMultiPlugin(MapSamlPlugin.meta_type)
context.registerClass(
MapSamlPlugin,
constructors=(manage_addMapSamlPluginForm, manage_addMapSamlPlugin),
permission=ManageUsers,
icon=os.path.join(mgr_dir, "saml_icon.png"),
visibility=None,
)
And this is also a standalone function "context" isn't derived from any imports or class.
The comment is an important clue:
def manage_addMapSamlPlugin(self, id, title='', delegate_path='', REQUEST=None):
""" Factory method to instantiate a MapSamlPlugin """
# Make sure we really are working in our container (the
# PluggableAuthService object)
self = self.this()
self is expected to be an object which has a this() method -- it sounds like that method returns a PluggableAuthService object. If you grep the rest of the code for def this you'll probably find it. Looking for class PluggableAuthService might also shed some light.
If you call this function and pass it a self that doesn't implement the expected interface, you'll get an AttributeError at runtime. Since there are no type annotations here, there's not really a way to catch errors statically (at "compile time" -- although typically compiling Python doesn't in itself enforce any static type checks).
My suspicion is that this function was originally a method of that class, and got refactored out of it for some reason (maybe as the first step in some larger refactor that was never finished). A class method works just fine if you yank it out of a class, provided that you explicitly provide the self parameter when you call it.

How to check if method exist from within a Python object

Context and intentions: I want to use an object m_o of type My_object as a way of interfacing with another object called s_o of type Stubborn_object. For the sake of easy understanding, they should behave like if My_object inherited from Stubborn_object, in the way that calling an attribute that doesn't exist in My_object should call the attribute in Stubborn_object.
However, the tricky thing is that I wouldn't be asking this question if I could simply inherit My_object from Stubborn_object. It appears that I can't inherit from it, and, for many reasons, I also can't modify the code of the Stubborn_object class, so I have to use it as it is. Please note that trying to inherit isn't the issue of the question here. I know that other solutions exist for my practical problem, but I really want answers to stay on topic for many reasons. I suspect that other users can have different problems than mine and still be unable to inherit a class. Furthermore, not being able to inherit a class is not the only reason that could make someone read this question. In fact, it's quite a general Python object-oriented problem. I also believe the solution of my problem could be useful in other applications, like custom error handling within the object itself when an attribute is not found, or in thread management to lock the instance as soon as an attribute is called.
In addition to the problem of inheritance, let's suppose that I can't use conditions at higher levels to handle these cases, so everything has to be done inside My_object instance or its parents. That means that I can't use hasattr(m_o, attribute_name) to determine if I should call getattr(m_o, attribute_name) or getattr(s_o, attribute_name). This also means that any try/except blocks and other preconditions must be inside the My_object class or its parents. The point of this question is not about detecting exceptions when calling an attribute from outside the My_object instance. A try/catch block normally has to be outside the My_object class, and I previously stated that this can't be allowed.
For the sake of clarity and to provide a complete verifiable example, here is a sample code of the Stubborn_object class. I know that I said I can't inherit from Stubborn_object and the following code includes an inheritable class. Providing an example of an non-inheritable object would only bring confusion and it would'nt be really helpful to the question anyway, so here is a simple example of an inheritable object. The objective of this is to make an easy to understand question, so please just consider that you can't inherit from it:
class Stubborn_object:
def do_something(self):
print("do_something")
def action_to_override():
print("action_to_override")
def action_a(self):
print("action_a")
def action_b(self):
print("action_b")
Objective: Put it simply, I want my class My_object to detect all by itself that a lacking attribute has been called and run some instructions instead of throwing an AttributeError.
Current attempts: Right now, I manually redirect method calls to the Stubborn_object instance like so (it's successful, but not reliable nor scalable because of the use of hardcoding):
class My_object():
def __init__(self, s_o):
self.stubborn_object = s_o
def action_to_override(self):
# Do stuff. This method "overrides" the Stubborn_object.action_to_override method.
print("Here is stuff getting done instead of action_to_override")
def action_a(self):
return self.stubborn_object.action_a()
def action_b(self):
return self.stubborn_object.action_b()
s_o = Stubborn_object()
m_o = My_object(s_o)
m_o.action_to_override() # Executes Stubborn_object.do_something()
m_o.action_a() # Executes Stubborn_object.action_a()
m_o.action_b() # Executes Stubborn_object.action_b()
Executing this code along with the provided Stubborn_object code sample should print:
Here is stuff getting done instead of action_to_override
action_a
action_b
As you can see from methods action_a and action_b, I have to manually call the Stubborn_object methods from whithin the methods in My_object to mimic the attributes of Stubborn_object. This is ineficient, lacks of robustness and will throw an AttributeError exception if we attempt to make an action that wasn't included in the My_object code.
What if I wanted to automatically send method and attribute calls to the Stubborn_object instance without having to rewrite all of its method and attributes in My_object? I believe this can be achieved with detecting if a lacking attribute of My_object instance is called.
Expectations (or sort of): I am open to any solution that allows the My_object class or its parents to determine if the attribute is lacking or not, all within itself. So I believe I am ready to hear extremely original ideas, so go ahead.
On my part, I believe that something that uses parts of this code is the way to go, but it still lacks the "catch any called attribute" part:
class My_object():
def __init__(self, s_o):
# __init__ stays as it was.
self.stubborn_object = s_o
def action_to_override(self):
# This method also stays as it was.
# Do stuff. This method "overrides" the stubborn_object.action_to_override method.
print("Here is stuff getting done instead of action_to_override")
def run_me_when_method_is_not_found(self, method_name, **kwargs):
print("Method " + method_name + " not in 'My_object' class.")
return getattr(self.stubborn_object, method_name)(**kwargs)
So running those lines with the previous code sample
s_o = Stubborn_object()
m_o = My_object(s_o)
m_o.action_to_override() # Executes Stubborn_object.do_something()
m_o.action_a() # Executes Stubborn_object.action_a()
m_o.action_b() # Executes Stubborn_object.action_b()
will print
Here is stuff getting done instead of action_to_override
Method action_a not in 'My_object' class.
action_a
Method action_b not in 'My_object' class.
action_b
Some similar methods will have to be made for getters and setters, however, the idea stays the same. The thing is that this code lacks the ability to detect that an attribute is missing.
Question: How can I run the run_me_when_method_is_not_found when the method is not found in My_object? Especially, how can a My_object instance detect that the method doesn't exists in its class instead of throwing an AttributeError exception?
Thanks a lot.
Seems like overriding __getattribute__ will do exactly what you want: search for attribute in self.stubborn_object if it is missing in self. Put it into My_object class definition:
def __getattribute__(self, attr):
try:
return object.__getattribute__(self, attr)
except AttributeError:
return object.__getattribute__(self.stubborn_object, attr)

Python's inheritance __init__ which self is this self?

I have a question about the init() and inheritance in Python. The following codes are from thinkbayes:
Suppose we have a parent class Pmf where it has functions such as Set() and Normalize(). Now we want to build another class called Cookie that inherits Pmf class.
class Cookie(Pmf):
def __init__(self, hypos):
Pmf.__init__(self)
for hypo in hypos:
self.Set(hypo, 1)
self.Normalize()
Here my question is that, since this is basically redefining a child class's init() after inheriting the init() from the parent class, we call:
Pmf.__init(self)
But, after this line,
for hypo in hypos:
self.Set(hypo, 1)
self.Normalize()
Is the "self" in self.Set(hypo, 1) and self.Normalize() the "self" from Pmf class or from the new Cookie class?
I am a little bit confused...
Thank you very much!
Is the self in self.Set(hypo, 1) and self.Normalize() the self from Pmf class or from the new Cookie class?
It's a false dichotomy: there is only one self. Its type is the class an instance of which is being created. In your example this is Cookie.
If you wrote c = Cookie, then self in both Cookie.__init__ and Pmf.__init__ refer to the same instance of Cookie. The type of self does not change based on which function is called.
The machinery that gets to __init__ is something like this: c = Cookie() (ignoring a couple of steps) first results in a call to Cookie.__new__(), which will return an instance of Cookie. That new instance is then passed to Cookie.__init__; that instance is what self refers to in Cookie.__init__. The type of that instance does not change when you make the call Pmf.__init__(self); it's still an instance of Cookie being further processed by Pmf.__init__.
First you should use the super function:
super(Cookie, self).__init__()
Second the self always refers to the instance of the object. The fact that you call a method from the self object means that you call the first method of that name found in the inheritance tree travelling upwards. So if the Cookie contains such a method, it will be called, and if it doesn't contain one the one contained in the inherited classes will be used. This is what the super does.
Also, there is a different syntax of super between Python3 and Python2

TypeError: QFileDialog.history(self): first argument of unbound method must have type 'QFileDialog'

I'm using PyQT and have already instantiated the class for my GUI. I'm trying to find the history of files opened in my GUI.
def plotButtonClicked(self):
lst = QFileDialog.history(self)
The above def is called by another function which creates the whole GUI. I get the following error:
TypeError: QFileDialog.history(): first argument of unbound method must have type 'QFileDialog'
Funny thing is I've been using the QFileDialog in other functions. Here's one instance where it works:
fname = QFileDialog.getOpenFileName(self, 'Open File')
So why isn't the history() method not working?
QFileDialog.history is not a classmethod (also called static function in C++).
For you to be able to call the method, you'd have to either pass the class as first argument (as the error says) or instantiate QFileDialog before calling the method.
The reason why QFileDialog.getOpenFileName works is because it's a classmethod (static method), and as the doc says, it's a convenience static function.

Categories

Resources