I have a class that is called that runs a while loop command prompt, i am using dir()
and getattr()
to dynamically create a list of methods for a command shell. I want to return values, but return from a dynamically called method just exits to main while loop, why how can i fix this?
class myClass :
def __init__(self) :
self.commands = []
for x in dir(self) :
k = getattr( self, x )
if hasattr(k, '__func__') :
self.commands.append(x)
# strips off __init__ and __main__
self.commands = self.commands[2:]
def help(self, args=None) :
for x in self.commands :
####
#### The issue is here
print('Command: {}'.format(x))
f = getattr(self, x)('-h')
desc = f()
print('Description: {}'.format(desc))
...
return SomeValue
def cmd_b(self, args=None) :
if args == '-h' :
return 'Some description'
...
return SomeValue
def cmd_c(self, args=None) :
...
return SomeValue
def __main__(self) :
while True :
command = input(self.ENV['PS1'])
command += ' '
command = command.split()
print(command)
if len(command) > 1 :
print(command)
print(len(command))
args = command[1:]
command = command[0]
else :
command = command[0]
args = None
if command == 'exit' :
break
if command not in dir(self) :
print("Command `{}` not found".format(command))
continue
print(command)
f = getattr( self, command )(args)
x = f()
getattr
and return valuesWhen you do
getattr(self, attr)
you get back the corresponding object and it's identical to calling the attribute directly. E.g:The following are equivalent
the variables
value
andtowels
are42
and"Remember your towel"
in both cases.This should respond to your questions.
HOWEVER:
The main problem in the code
However the main problem in the code has nothing to do with the
getattr
.In the
help
method you refer to af
function that you never define, so that raises an exception.But the big problem is at the end of
__main__
:at the end. You should never ever do this: you are silencing errors, making it very hard to debug.
If you want to make your code resilient to errors you should catch all the errors and report them somewhere, e.g. using log messages:
If you do somethign like this in your code, it would be much easier to catch errors like the undefined
f
function.Some suggestions:
In the
help
function, you loop over all the commands,help
itself included, and print their help. The way you have implemented the help, this causes an infinite recursion. You should skip callinghelp
from thehelp
function.all the commands return
None
or a string. When you will fix the code as indicated above, you will get a lot of errors from line 63:f()
ps: If you are starting with python, give a look at PEP8, the official python style guidelines