#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    24
    Rep Power
    0

    A noob question.(Calling function)


    Hi, I would like to know why it doesn't work:

    Code:
    class Hello:
        def abc(self, obj):
            print "Hello"
    
    if __name__ == "__main__":
        d = 5
        abc(d)
    and this:
    Code:
    class Hello:
        def abc(self, obj):
            print "Hello"
    
    if __name__ == "__main__":
        d = 5
        Hello.abc(d)
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    The first example doesn't work because abc() is a method of the Hello class and not a function. You need to call it on an instance of a the Hello class.

    Code:
    class Hello:
        def abc(self, obj):
            print "Hello"
    
    if __name__ == "__main__":
        d = 5
        Hello().abc(d)
    Alternatively you could create a class method by using the @classmethod decorator and call it like you do in your second example.

    Code:
    class Hello:
        @classmethod
        def abc(self, obj):
            print "Hello"
    
    if __name__ == "__main__":
        d = 5
        Hello.abc(d)
    If on the other had you wanted a function then you need to define abc outside of the class (without self). This can then be called just like you wanted to do in your first example:

    Code:
    def abc(self, obj):
        print "Hello"
    
    if __name__ == "__main__":
        d = 5
        abc(d)
    Hope this helps,

    Mark.
    programming language development: www.netytan.com Hula

  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    24
    Rep Power
    0
    Originally Posted by netytan
    The first example doesn't work because abc() is a method of the Hello class and not a function. You need to call it on an instance of a the Hello class.

    Code:
    class Hello:
        def abc(self, obj):
            print "Hello"
    
    if __name__ == "__main__":
        d = 5
        Hello().abc(d)
    Alternatively you could create a class method by using the @classmethod decorator and call it like you do in your second example.

    Code:
    class Hello:
        @classmethod
        def abc(self, obj):
            print "Hello"
    
    if __name__ == "__main__":
        d = 5
        Hello.abc(d)
    If on the other had you wanted a function then you need to define abc outside of the class (without self). This can then be called just like you wanted to do in your first example:

    Code:
    def abc(self, obj):
        print "Hello"
    
    if __name__ == "__main__":
        d = 5
        abc(d)
    Hope this helps,

    Mark.

    Hello,

    Ok, i've a big problem now.

    In my class, I've a constructor. so if I do example one:
    Code:
    class Hello:
        suprevalue = ""
        def __init__(self, a, b):
            *calling other methods*
        def abc(self, obj):
            self.supervalue = "yay"
            print self.supervalue
        (Other methods)
    
    if __name__ == "__main__":
        d = 5
        Hello(123,235)
        Hello().abc(d)
    It won't work...since Hello().abc(d) HAVE TO go through the constructor which I dont' want. I want it goes directly to that def abc method.
    Also, supervalue is a variable that use by other methods in the class too.

    Also, your example 2 seem not working..I got an error message saying "@" is not valid syntax.
    *edit*
    I think I just found out that '@' is for Python 2.4, unfortunately, my program HAVE TO work for older version of Python too :/
  6. #4
  7. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    The class is being called on an instance of Hello(), because creating an instance automatically calls the __init__ method the only way to get around it would be to not have this method. Better yet you could initialise your self.supervalue variable there .

    Right now I would expect a syntax error from Python when creating an instance.

    You got it, function decorators are new in Python 2.4 but you can use the classmethod() function to create a class method in older versions of Python i.e.

    Code:
    class Hello:
        def abc(self, obj):
            print "Hello"
        abc = classmethod(abc)
    
    if __name__ == "__main__":
        d = 5
        Hello.abc(d)
    Hope this helps,

    Mark.

    Comments on this post

    • sf2k disagrees
    programming language development: www.netytan.com Hula

  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    24
    Rep Power
    0
    Thx

    Okay, but I just found out another way, but I'm not sure if it is good or not.

    I put that method outside the class, so method abc become a function. And I make variables in the class to outside, so it become global variable. So now I can call that abc() function from main, because it's now a function instead of a method. and it can access the variable, because it is a global variable.
  10. #6
  11. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    So your going for a function rather than a class. Probably a good idea since the class may have been overkill .

    Have fun,

    Mark.
    programming language development: www.netytan.com Hula


IMN logo majestic logo threadwatch logo seochat tools logo