Point of parentheses


#1

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


#3

https://www.codecademy.com/courses/python-intermediate-en-WL8e4/0/1

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

lemon.description()

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.


#4

Thank you very much, this helps alot.

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


#5

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.


#6

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