Skip to content

Variables in Python

variables are memory references, not equal to the object but reference(alias) the object at memory space.

Find out memory address referenced: using id()

reference counting

after we created a object in memory, python keep track of the number of the references we have to the object, as soon as the count goes to 0, python memory manager destroy object and reclaim the memory space

Find out reference count:

  • using sys.getrefcount()
  • using ctypes.c_long.from_address()

circular references

in the circumstance of circular reference, the reference count could not goes to 0(memory leak), need garbage collector to identify it

garbage collection

can be control programmatically using the gc module, turned on by default, beware to turn it off, for python < 3.4, if even one of the objects in the circular reference has a destructor, the destruction order may be important, but the GC does not know what order should be, so the circular reference objects will be marked as uncollectable and cause memory leak

dynamically typing

python variable name has no references to any type, when we use type(), python looks up the object which is referenced and return the type of the object

variable equality

  • identity operator(var_a is var_b) compare the memory address
  • equality operator(var_a == var_b) compare the object state

Keyword “this”

JavaScript engine every time an execution context is created, that is every time a function is RUN, it gives a variable called “this” pointed to a different object depends on how the function is invoked(where the function is and how it been called)

Weird part

 

By Value v.s. By Reference

by value (primitive type)

copying the value into two separate spots in memory

by reference (all objects)

no copy of object is created, instead, two names or more point to same address

Function in JavaScript

JavaScript functions are objects that has other special properties:

  • Name: optional, can be anonymous
  • Code: invokable, you can RUN these code by ()
Function statement

Create a object with NAME attribute and point to memory in the creation of execution context

Function expression (anonymous)

Create a object without NAME(already have a variable knows where this object lives) attribute and point to memory in the creation of execution context, and assign this object to function when this line of code is execute

Immediately Invoked Function Expressions (IIFE)

Create the function object on the fly and invoke it immediately, and assign what it return to the variable.

Why use iife and why it is safe

Code wrapped in IIFE does not interfere with, crash into, or be interfered by any other code included in the application.

Methods

In one function’s execution context, we have 1. Variable Environment 2. Outer Environment 3. “this”.

Q: How to control “this” variable?

JavaScript functions also has these methods(can control “this”):

  • call()
  • apply()
  • bind()

Functional Programming

We should think and code in terms of functions

Type in JavaScript

primitive value
  • undefined
  • null
  • boolean
  • number (only one number type…)
  • string
  • symbol (ES6)
coercion

expected

weird parts

loose equal vs strict equal

use === to prevent coercion and do not use == in 99% cases except you mean to coerce

ref:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness

Python Name Conventions

Must start with (_) or letter (a-z, A-Z), follow by any number of (_) or letter(a-z, A-z) or digit (0-9) except reserved words

Conventions

_my_var: indicate “internal use” or “private” object, cannot get imported by

__my_var: used to mangle class attributes, useful in inheritance chain

__my_var__: system defined

PEP8 style guide