1

Possible Duplicate:
what is difference between init and call in python?

I'm trying to create a callable class which will take arguments, then return itself as an object.

class User(object):
    def __init__(self, loginName, password):
        self.loginName = loginName

    def __call__(self):
        if self.login():
            return self
        return None

    def login(self):
        database = db.connection
        realUser = database.checkPassWord(self.loginName, self.password)
        return realUser

My questions are, if I call this object like so:

newUserObject = User(submittedLoginName)

Will __init__ get called BEFORE __call__? Should __init__ get the argument or should I be moving the argument over to __call__ like

def __call__(self, loginName):
Community
  • 1
  • 1
zakdances
  • 22,285
  • 32
  • 102
  • 173
  • That doesn't answer my question...I'm trying to understand the relationship between them, not just "what's the difference" – zakdances Sep 23 '12 at 17:16
  • @yourfriendzak I think you need a factory function that your client calls - that way they won't need to know about the `User` class. Anyway your `User` class has too many responsibilities if it has to log itself in and then respond to further calls by your client. – quamrana Sep 23 '12 at 17:23
  • @quamrana You're right...I'm gonna remove the __call__ and just use __init__ – zakdances Sep 23 '12 at 17:27

1 Answers1

4

__call__ is only called on an instance that defines itself as callable.

__init__ is the initializer that provided an instance of the class

If you do something like

MyObject()() Then you are initliaizing THEN calling.

Using your own example

class User(object):
    def __init__(self, loginName, password):
        self.loginName = loginName
        self.password = password

    def __call__(self):
        if self.login():
            return self
        return None

    def login(self):
        database = db.connection
        return database.checkPassWord(self.loginName, self.password)

a = User("me", "mypassword")
a = a() # a is now either None or an instance that is aparantly logged in.
Jakob Bowyer
  • 33,878
  • 8
  • 76
  • 91
  • Can you change your example so that the class User will return an initialized object of itself with all the same arguments? That's what I'm trying to do. – zakdances Sep 23 '12 at 17:17
  • @yourfriendzak: Perhaps you should mark this as the answer and ask another question with more details of what you mean by "return an initialized copy of itself with all the same arguments" – Mark Hildreth Sep 23 '12 at 17:18
  • @yourfriendzak your design seems a bit awkward anyway. – Jakob Bowyer Sep 23 '12 at 17:19
  • @JakobBowyer I'm trying my best here. Now that I look at it, I'm thinking my __call__ is unnecessary. I can just pass arguments to the class when initializing. – zakdances Sep 23 '12 at 17:21
  • @yourfriendzak correct, pass them as arguments then get `.login()` to raise an exception if it fails. Remember to mark as answer before you leave. – Jakob Bowyer Sep 23 '12 at 17:22