I started reading/executing the examples given in Dive into Python 3 book by Mark Pilgrim. After reading the first chapter I felt that I need to take some notes on the basics. Some of the basics may be rarely remembered or talked once you make progress on coding in python. I cautiously look for those kind of points and list here.
- Everything in Python is an object. Strings are objects. Lists are objects. Functions are objects. Classes are objects. Class instances are objects. Even modules are objects.
My first thought was, What?. The example given in the book was:
All functions have a built-in attribute
__doc__, which returns the docstring defined in the function’s source code. The
sysmodule is an object which has (among other things) an attribute called path.
You may have heard the term “first-class object” in other programming contexts. In Python, functions are first-class objects. You can pass a function as an argument to another function. Modules are first-class objects. You can pass an entire module as an argument to a function. Classes are first-class objects, and individual instances of a class are also first-class objects.
- Every function deserves a decent docstring.
- All names in Python are case-sensitive: variable names, function names, class names, module names, exception names. If you can get it, set it, call it, construct it, import it, or raise it, it’s case-sensitive.
- The below statement is like ternary operator in C (?:)
multiple = 1024 if a_kilobyte_is_1024_bytes else 1000
- In Python, variables are never explicitly typed. Python figures out what type a variable is and keeps track of it internally.
- Python allows function arguments to have default values; if the function is called without the argument, the argument gets its default value. This means the argument is optional;
- You can also pass values into a function by name. These are received as key-word arguments. If you pass just a value they are recieved as positional arguments. Positional arguments should be passed in the order. We should pass them first and then the named arguments. Reading the argument list from left to right, once you have a single named argument, the rest of the arguments must also be named.
- i = 10, In python, ‘i’ is a
objectcreated by interger 10. In C langauge, ‘i’ is pointing to a memeory location where the integer value 10 is stored. C developers has to keep this point in mind.