This forum is now read-only. Please use our new forums at discuss.codecademy.com.
Strings & Console Output Forum
I don't understand Dot Notation
I don't understand it at all! Will someone talk me through it please?
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 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
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
"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
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.
lenis 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
lenand other such built-in global functions are part of the
__builtin__module. Not important to know though...
- There are many kinds of built-in classes, the most common being
Dict. Each of these builtin classes have associated methods that are defined in the source code.
- 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
basicmathand another called
advancedmathand they both have constants
PI, but with different values, then I would call
basicmath.PIto get that version of PI, and
advancedmath.PIto get its version of PI.