Summary

  • Python object references

    • Think of Python working in terms of named references to objects rather
      than variables and values.

    • Assignment doesn't put a value in a box. It attaches a name tag to an
      object.

    • Assigning from one reference to another puts two name tags on the same object.

    • The Python garbage collector will reclaim unreachable objects - those
      objects with no name tag.

  • Object identity and equivalence

    • The id() function returns a unique and constant identifier but should rarely, if ever, be used in production.

    • The is operator determines equality of identity. That is, whether two
      names refer to the same object.

    • We can test for equivalence using the double-equals operator.

  • Function arguments and return values

    • Function arguments are passed by object-reference, so functions can modify their arguments if they are mutable objects.

    • If a formal function argument is rebound through assignment, the reference to the passed-in object is lost. To change a mutable argument you should replace its contents rather than replacing the whole object.

    • The return statement also passes by object-reference. No copies are made.

    • Function arguments can be specified with defaults.

    • Default argument expressions are evaluated only once when the def
      statement is executed.

  • The Python type system

    • Python uses dynamic typing, so we don't need to specify reference types in advance.

    • Python uses strong typing. Types are not coerced to match.

  • Scopes

    • Python reference names are looked up in one of four nested scopes
      according to the LEGB rule: Local to functions, in Enclosing functions, in the Global (or module) namespace and Built-ins.

    • Global references can be read from a local scope

    • Assigning to global references from a local scope requires that the
      reference be declared global using the global keyword.

  • Objects and introspection

    • Everything in Python is an object, including modules and functions. They can be treated just like other objects.

    • The import and def keywords result in binding to named
      references.

    • The built-in type() function can be used to determine the type of an object.

    • The built-in dir() function can be used to introspect an object and
      return a list of its attribute names.

    • The name of a function or module object can be accessed through its
      __name__ attribute.

    • The docstring for a function or module object can be accessed through its __doc__ attribute.

  • Miscellaneous

    • We can use len() to measure the length of a string.

    • If we "multiply" a string by an integer we get a new string with multiple copies of the operand string. This is called the "repetition" operation.

  1. You'll notice that here we've referred to the object reference with the name x as x. This is admittedly a bit sloppy since, of course, x will generally mean the object referred to by the object reference with the name x. But that's a mouthful and a bit overly pedantic. Generally speaking, the context of the use of reference names will be sufficient to tell you whether we mean the object or the reference.

  2. Garbage collection is an advanced topic that we won't cover in this book. In short, though, it's the system by which Python deallocates and reclaims resources (that is, objects) which it determines are no longer in use. 
  3. Since assigning a list reference to another name doesn't copy the list, you may be wondering how you could make copy if you wanted. This requires other techniques which we'll look at later when we cover lists in more detail.

  4. Note, however, that Python does not enforce this behavior. It is entirely possible to create an object which reports that it is not value-identical to itself. We'll look at how to do this — should you for some reason feel the urge — in later chapters.

  5. Though there's no universally accepted terminology, you'll often see the term parameters or formal parameters used to mean the names declared at the function definition. Likewise, the term arguments is often used to mean the actual objects passed into a function (and, thus, bound to the parameters). We'll use this terminology as needed throughout this book.

  6. And this behavior is part of the syntax implementation, not the type system.

 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset