Why am I getting an AttributeError when I have the attribute?
-
23-09-2019 - |
Question
I keep getting the following error:
AttributeError: Caribou instance has no attribute 'on_key_up'
The problem is, I'm pretty sure I do have that attribute...
Here are some excerpts from my code (from caribou.py):
def on_key_up(self, event):
if event.event_string == "Shift_R":
_r_shift_down = False
elif event.event_string == "Shift_L":
_l_shift_down = False
And this is the line that is causing the error:
pyatspi.Registry.registerKeystrokeListener(caribou.on_key_up, mask=None, kind=(pyatspi.KEY_RELEASED_EVENT,))
Anybody see what I'm doing wrong?
Thanks!
edit: Whoops--here's how I create the caribou instance:
caribou = Caribou()
Solution
The OP mentions in a comment that dir(caribou)
gives him:
['__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__']
so it definitely looks at that point that caribou
is a module -- nothing else would normally have __builtins__
etc. The error message however clearly mentions a Caribou instance
-- so I imagine that something else must be happening between that dir
call and the following attempt to access caribou.on_key_up
.
Clearly the OP is having some multiple use of that beloved caribou
identifier (at some point it's bound to a Caribou instance, but at other times it's clearly a module, and indeed the OP does mention a caribou.py
which is clearly going to become a module named caribou
when imported).
So my recommendation is to clarify naming. For example, use
caribou_instance = Caribou()
instead of binding one more value to the caribou
name, and replace all uses of caribou
which are supposed to be the instance (not the module) with caribou_instance
. That may give you a different error, which could be more informative.
OTHER TIPS
What happens if you print dir(caribou)? Do you see your method?
You aren't showing your import statements or how "caribou" the instance is being created. My guess is you are trying to pass caribou.on_key_up as in caribou the module, not the instance.