Object Mutability in Python

Internal state

changing the data inside the object is called modifying the internal state of the object, the state(data) is changed, but memory address has not changed

Mutable

an object whose internal state can be changed

  • Lists, Sets, Dictionarys, User-defined Classes

Immutable

an object whose internal state can not be changed

  • Numbers(int, float, Booleans), String, Tuples, Frozen Sets, User-defined Classes
  • variable re-assignment change the reference not the internal value

 

發表留言

Keyword “Arguments” and Spread

“arguments” is another keyword JavaScript engine create during the creation of execution context, arguments contains a list of all the values of all the parameters that passed to a function.

argument is an array-like object, it doesn’t has all the features of JavaScript array

“arguments” will become deprecated in the future, the new thing is called spread

 

發表留言

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

發表留言

Python Multi-line Statements

How python interpret multi-line code into single line code:
  1. python program
  2. physical lines of code(end with a physical newline CHARACTER create by enter)
  3. logical lines of code(end with a logical NEWLINE token)
  4. tokenized
  5. execute
physical newlines vs logical newline

sometimes physical newlines are ignored in order to combine multiple physical lines into a  single logical newline

break implicitly: [], (), {}

break explicitly

multi-line strings

multi-line strings are regular string, not comments (can be used as docstring)

escaped characters(\n, \t), non-visible characters(newlines, tabs) in multi-line are part of string; escaped characters will formatted when print it

ref:

https://github.com/fbaptiste/python-deepdive/blob/master/Part%201/Section%2002%20-%20A%20Quick%20Refresher/01%20-%20Multi-Line%20Statements%20and%20Strings.ipynb

發表留言