Dive into python – 1

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.

Chapter 1

  1. 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 sys module 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.

  2. Every function deserves a decent docstring.
  3.  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.
  4. The below statement is like ternary operator in C (?:)
    multiple = 1024 if a_kilobyte_is_1024_bytes else 1000
  5. In Python, variables are never explicitly typed. Python figures out what type a variable is and keeps track of it internally.
  6. 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;
  7. 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.
  8.  i = 10, In python, ‘i’ is a name with a binding to the object created 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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s