Point of parentheses


Hi, sorry i'm just a little confused to the point of parentheses.

What is the reason that you use it in situations such as this

lemon.description() <--

Also, what is the point of self? Does self point to the class fruit or the fruit that you apply to it. Such as lemon earlier. Thanks



As it is written, lemon is an instance of a class, and .description() is a method of the class.


is invoking that method on the lemon instance.

class Fruit
    def __init__(kind, describe):
        self.kind = kind
        self.describe = describe

    def description():
        print self.describe

lemon = Fruit('lemon', 'yellow, citrus')

lemon.description()    # yellow, citrus

self is the placeholder variable within the class that points to the current instance context. Inside the above method, self would be lemon. It goes deeper than this, but that is the basics of it.


Thank you very much, this helps alot.

Also, is the self necessary in all methods or is it implied and can be left out?


No, it is not implied. It if is left out then Python will be looking somewhere else for the method, not in the class. It would then be a function, as opposed to being a method.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.