Strings

← Forum
219 pts

a_sharma10

I don't understand Dot Notation

I don't understand it at all! Will someone talk me through it please?

  • 1693 pts

    Michael Rochlin

    18 votes

    Sure (note that the following applies specifically to Python but is also true of many other languages). Python, like many programming languages, supports Object Oriented Programming or OOP for short. In this paradigm (type of programming), we approach ideas as Objects much as we do in the real world. Each Object is an instance of a Class or a type of object. For example, I am Michael Rochlin. I am a person. I am also a Codecademy member and also a Codecademy Moderator. So we could say that there is an object, which we will call michael_rochlin and it is an instance of a class person.

    Now every person can say his/her name so we might have a function or method that tells the person to say his/her name. That method would be called say_name. It would be written inside the definition of the class person so that every person could use it.

    Then we would create an instance of person and name it michael_rochlin. This instance is only one person and we could have other instances like zach or eric or linda(who are all Codecademy staff members BTW.)

    So what does all this have to do with dot notation? Dot notation allows us to tell a instance of a class to use one of the methods inside that class. So if we called michael_rochlin.say_name() it would call the method say_name() that was written in the person class because michael_rochlin is a person.

    "But Michael, we haven't learned anything about classes or OOP or objects or anything like that. What are you talking about!??"

    In Python, almost everything you use is an object. You have used at least seven different classes of Objects. You have used Integers, Floats, Strings, Lists, Tuples, Dictionaries and Booleans. (don't worry if you haven't, you will) These are all special classes in Python and each has it's own methods.

    One method of the List class is append() which appends a value to a list. What you are really doing is calling the method append from the List class on that object.

    (There is a similar idea by modules, but its practically the same...)

    Hope that helped, even if it was a little complicated. If you want more help, let me know

    Show all comments
    • Simon Bhattacharya over 1 year ago
      @corner xu hmm, you cannot pass an argument in upper() function, say for an example if you try to run this code >>> x = "this is in small caps" y = "hmmm" # now if you ttry to print x.upper(y) you will get a error saying as TypeError: upper() takes no arguments (1 given) if that's what you meant. @michael thanks for the brief answer, quite a bit helpful in understanding OOP
    • santoshnnag over 1 year ago
      i like Michael's explanation...awesome...codecademy is gonna change my life...thanks
    • Timo J over 1 year ago
      This was beautiful <3
  • 1693 pts

    Michael Rochlin

    5 votes

    So here's a simpler explanation. There are two kinds of functions (actually one is technically a method but we'll ignore that). One kind of function takes an argument by putting that argument inside the parentheses. For example - len("mystring") will return 8 because there are 8 letters in "mystring". (See footnote one for slightly more detail)

    Another kind of function is one that is specific to a type or class of objects. An example of a class is List. An example of a List is [1,2,3,4]. The class List has functions defined that work specifically on Lists. To use those functions on a specific List we would use dot notation. For example, to use the special function append on a List called my_list we would write my_list.append(insert_thing_to_append). (See footnote 2 for a little more)

    Hope that is a simpler way of explaining it. You will come across dot notation by functions for Lists, Dictionaries and Strings quite often. You will also use them for user-defined classes and even for using specific functions from modules and libraries.


    Footnotes:

    1. len is an example of a function that is defined in the __builtin__ namespace which is the environment you are working in. Almost everything in Python is actually an object and therefore part of a class or a module. So technically len and other such built-in global functions are part of the __builtin__ module. Not important to know though...
    2. There are many kinds of built-in classes, the most common being List, String and Dict. Each of these builtin classes have associated methods that are defined in the source code.
    3. In general, dot notation tells Python to look inside the space that is before the dot for code to execute. You can use dot notation to access the specific version of a certain function that is defined in a different class or a different module. For example, if I have one module that is called basicmath and another called advancedmath and they both have constants PI, but with different values, then I would call basicmath.PI to get that version of PI, and advancedmath.PI to get its version of PI.
    • Test_man over 1 year ago
      This is helpful (especially along with the answer from Michael), but what is the best way to get to know when to use either? Or is it rote memorization?
    • Timo J over 1 year ago
      Can this be included in the lesson? As in this explanation.

Sign in to post to the forum.

Sign in