views:

29

answers:

1

I'm really new to programming... I set up a class to give supporting information for Google's User API user object. I store this info in the datastore using db.model.

When I call the okstatus method of my user_info class using this code:

elif user_info.okstatus(user):
    self.response.out.write("user allowed")

I get this error:

unbound method okstatus() must be called with user_info instance
as first argument (got User instance instead)

Here is my user_info class.

class user_info:
    def auth_ctrlr(self, user):
        if self.status(user) == status_allowed:
            return ("<a href=\"%s\">Sign Out</a>)" %
                (users.create_login_url("/")))
        else:
            return ("<a href=\"%s\">Sign In or Get an Account</a>)" %
                (users.create_logout_url("/")))
    def status(self, user):
        match = sub_user.gql(qu_by_user_id, user.user_id)
        return match.string_status
    def group(self, user):
        match = sub_user.gql(qu_by_user_id, user.user_id)
        grp = group_names.gql(qu_by_user_id, match.groupID)
        return grp
    def okstatus(self, user):
        match = self.status(user)
        if match == status_allowed:
            return True

My understanding is that the argument "self" inside the method's calling arguments describes it as a child to the class. I've tried everything I can think of and can't find any related info online. Can someone please tell me what I'm doing wrong?

Thanks

+1  A: 

self must be an instance of the class. Since you never actually use it, you can simply make all of these methods into functions (and changing the self.status cases to just status).

If you're a "class fetishist", and absolutely insist on keeping the functions as methods in a class (rather than the module top-level functions they "want" to be!-), then change the call site to

elif user_info().okstatus(user):

that is, make an instance of your user_info class, and call the method on it (the useless instance then immediately goes away, pointing out that these should really be functions;-).

Alex Martelli
You are very correct. Thanks for helping me really understand the situation. I'll take your advice and functionize them.
Spencer Leland