Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Treat error ?
#11
(Apr-17-2020, 07:30 AM)buran Wrote: You were asking about hasattr, your code example uses getattr.
Your code is fine up to line 14. from there on, line by line:
o_method = getattr (o_class, v_metget, None)
you try to get non-existing method. You also supply optional default value of None.

o_return = o_method ()
o_return is None, but you try to call it, so you get the current error - TypeError, which is different from the original error you had - AttributeError. I guess in your original code you were not supplying None.

So you can do three things, one is to use hasattr, as per your initial question:
# Native Module Import: import_module
from importlib import import_module
 
v_appget = "website"
v_conget = "runcode"
t_conget = "Runcode"
v_metget = "scree" # method does not exist
 
# Import dynamic module: calling the controller and pattern method:
v_pathmod = "apps." + v_appget + ".controllers." + v_conget
# Import dynamic module: passing as path the path assigned to the variable:
o_module = import_module (v_pathmod)
# Retrieve the class: passed as an attribute parameter in the class-controller variable:
o_class = getattr (o_module, t_conget)
# Retrieve the method: passed as an attribute parameter in the method-controller variable:
has_o_method = hasattr(o_class, v_metget)
print(has_o_method) # this will print False
if has_o_method:
    o_method = getattr (o_class, v_metget)
    # Instantiate the object: related to the class method:
    o_return = o_method()
Second is to use the default value of None:
# Native Module Import: import_module
from importlib import import_module
 
v_appget = "website"
v_conget = "runcode"
t_conget = "Runcode"
v_metget = "scree" # method does not exist
 
# Import dynamic module: calling the controller and pattern method:
v_pathmod = "apps." + v_appget + ".controllers." + v_conget
# Import dynamic module: passing as path the path assigned to the variable:
o_module = import_module (v_pathmod)
# Retrieve the class: passed as an attribute parameter in the class-controller variable:
o_class = getattr (o_module, t_conget)
# Retrieve the method: passed as an attribute parameter in the method-controller variable:
o_method = getattr (o_class, v_metget, None)
if o_method: # None is evaluated as False, so if method does not exists, it will skip calling it
    # Instantiate the object: related to the class method:
    o_return = o_method()
The third is to use try/except, i.e. EAFP - Easier to ask for forgiveness than permission, (that would be my preference as more idiomatic python) vs LBYL - Look before you leap, that is what using hasattr approach is.

# Native Module Import: import_module
from importlib import import_module
 
v_appget = "website"
v_conget = "runcode"
t_conget = "Runcode"
v_metget = "scree" # method does not exist
 
# Import dynamic module: calling the controller and pattern method:
v_pathmod = "apps." + v_appget + ".controllers." + v_conget
# Import dynamic module: passing as path the path assigned to the variable:
o_module = import_module (v_pathmod)
# Retrieve the class: passed as an attribute parameter in the class-controller variable:
o_class = getattr (o_module, t_conget)
# Retrieve the method: passed as an attribute parameter in the method-controller variable:
try:
    o_method = getattr (o_class, v_metget, None)
    # Instantiate the object: related to the class method:
    o_return = o_method()
except AttributeError:
    print (f'{o_mtehod} does not exists')
To sum up - Your initial problem with AttributeError was, because you were calling getattr on non-existing method, before callinh hasattr on it.
In the latest example you were getting TypeError, because you were calling getattr with default value of None (this was missing in your initial code).

And note - you show just the final line of traceback, not the whole traceback. Please, always post the full traceback, not just the final line.

Grateful buran, your first example managed to solve the logic was good, he was hot-headed and for that reason he was unable to follow up, thank you.
Reply
#12
Final note:
o_return = o_method()
this assume o_method is callable. If it is not you will get a TypeError.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  how to treat two token as one token(lexer)? hsunteik 1 3,538 Dec-28-2016, 12:26 AM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020