[Tutor] How to use a str object, to find the class in exact name?
Yaşar Arabacı
yasar11732 at gmail.com
Mon Mar 14 23:41:00 CET 2011
Before being able to see 3rd answer, I did something like this and it
worked for me:
#!/usr/bin/env python
# -*- encoding:utf-8 -*-
def command_dispatcher():
"Takes comman line arguments and executes regardin method"
command = raw_input(">>>")
args = command.split(" ")
query_string = ".".join(args[0:2])
method_args = ",".join(args[2:])
try:
exec query_string+"("+method_args+")"
except NameError:
print "You are trying to get somethin that is not exist!"
class calculate:
@staticmethod
def bundle():
print "your best bundle is -->"
command_dispatcher()
But after reading 3rd answer regarding the danger of exec, I had my
doubts, Now I will try to implement what second person said about
getattr, and will return here with result.
And What I want to do is a small economy application. It will work this way:
user add a product name, rate how much it like it over 10, and enter its
price.
[repeated as many times as wanted]
user will enter h(is|er) budget.
user will use "calcute bundle" to show much of each product s?he should buy
user will repeat any step as long as s?he likes.
15-03-2011 00:14, Steven D'Aprano yazmış:
> Yaşar Arabacı wrote:
>> Hi
>>
>> I am trying to do something like this:
>
>
> If you are trying to write a mini-language, the ``cmd`` and ``shlex``
> modules in the standard library may be useful to you.
>
>
>> #!/usr/bin/env python
>> def command_dispatcher():
>> "Takes comman line arguments and executes regardin method"
>> command = raw_input(">>>")
>> args = command.split(" ")
>> args[0].args[1]
>
>
> The error you get tells you exactly what is wrong:
>
> AttributeError: 'str' object has no attribute 'args'
>
>
> When you split the line, you get a list of strings. You extract the
> first string using args[0], and then look up the attribute "args" on
> that string, and finally look up the second element of that result. But
> strings don't have an attribute called args.
>
> Try this instead:
>
> getattr(args[0], args[1])
>
> When you do that, you'll run into a bunch of new and exciting errors,
> starting with the fact that nothing appears to happen. Try it and see.
>
> What you will eventually need to do is:
>
> * create a class instance and store it somewhere
> * call the method on the class instance
> * print its output
>
> none of which is yet done by your script. So far, all it does it try to
> look up a method on a class, then throw the result away without doing
> anything :)
>
>
>> class calculate:
>> def bundle(self):
>> print __class__
>> command_dispatcher()
>
>
> calculate.bundle won't do anything except fail, because __class__ is not
> a local variable.
>
> It's better to have the methods return a result, rather than print, and
> then have the command_dispatcher responsible for printing it. That lets
> you chain methods to make more powerful functions.
>
>
> Good luck, and have fun!
>
>
>
More information about the Tutor
mailing list