As I try to implement things with getattr, I am getting a really strange error. This is my file:

#!/usr/bin/env python
# -*- encoding:utf-8 -*-
class global_variables:
        "Holds class attributes, so that other classes can share them"
        products = 0
        best_bundle = []
class dispatcher:
        def GetMethod(self,class_name,method_name):
                """This method first finds a class if desired classexists.
                Then, instansites it, and returns a reference to desired method 
of
                the instance it created.
                """

                from sys import modules
                module = modules[self.__module__]
                if hasattr(module,class_name):
                        print "#debug : hasattr is true"
                        cls = getattr(module,class_name)
                else:
                        print "#debug : hasattr is false"
                        return None

                "if we get a valid class, lets instantie it"
                if cls:
                        a=cls()
                else:
                        return None
                return hasattr(a,method_name) and getattr(a,method_name) or None

        def dispatch_command(self):
                """Gets command from user, finds appropriate Class/method to run
                and then runs it. Beware of the fact that, to be able to 
successfully
                run the method, method should take exactly two arguments,
                arg 1: instance of class which method resides (e.g. self)
                arg 2: list of other needed variables

                list of other variables can be used to get as many variables as 
possible
                """
                
                command = raw_input(">>>")
                args = command.split(" ")
                if len(args) < 2:
                        return None
                method = self.GetMethod(args[0],args[1])
                return method and method(args[2:]) or None

class calculate(global_variables):
        def bundle(self,args):
                print "your best bundle is -->"

a = dispatcher()
a.dispatch_command()

I wanted to see what happens when someone gives an nonexistent function. But when I put a b, it gives me error, when I put c d it doesn't. I don't have either a or c classes, but a somehow causes problems :S This is what I did:

yasar@yasar-laptop:~/best_buy> ./main.py
>>>a b
#debug : hasattr is true
Traceback (most recent call last):
  File "./main.py", line 57, in <module>
    a.dispatch_command()
  File "./main.py", line 44, in dispatch_command
    method = self.GetMethod(args[0],args[1])
  File "./main.py", line 25, in GetMethod
    a=cls()
AttributeError: dispatcher instance has no __call__ method
yasar@yasar-laptop:~/best_buy> ./main.py
>>>c d
#debug : hasattr is false

_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to