Commits

Tom V committed 5147292 Merge

remove old jso.py

  • Participants
  • Parent commits 4d70388, e30e07c

Comments (0)

Files changed (3)

    gotcha-quiz
    gotcha-answers
 
+   know-in-python
+
    counters-example
    counters-discuss
    counters-memory

source/know-in-python.rst

+What do you need to know in Python
+==================================
+
+Global and local scopes
+-----------------------
+
+Whenever you reference a name in Python, it looks for it in several scopes. First it tries to find it in the *local* scope, which is usually the body of the function that is being currently executed. If the name is not there, Python tries the *global* scope, which is the body of the module. When that fails, it tries the *buildins*.
+
+.. code-block:: python
+
+    import os
+    # The name *os* gets defined in the *global* scope.
+
+    g = 3
+    # Define variable *g* in the *global* scope with value 3.
+
+    def my_function(x):
+        # The name *x* gets defined in *local* scope of *my_function*
+
+        # *x* is found in the *local* scope and its value is
+        # assigned to new variable *y* created in *local* scope.
+        y = x
+
+        # The name *os* is not found in the *local* scope, so the *global*
+        # scope is tried. A new variable *os* is created in the *local* scope
+        # with value of the *os* from the *global* scope.
+        os = os
+
+        # The name *g* from *global* scope is imported into the *local* scope.
+        global g
+
+        # The *global* variable *g* is assigned the value of *local*
+        # variable *x* 
+        g = x
+
+    my_function(3) 
+
+In JavaScript you have a whole hierarchy of scopes -- each new nested function
+creates a new scope. The scopes are searched beginning from the innermost up to
+the global object.
+
+
+Default scope
+-------------
+
+In Python, when you create a new name, it goes by default to the *local* scope
+(when you are at the module scope, it's equal to the *global* scope).
+You don't need to do anything special to make that happen.
+
+In JavaScript, variables go to the global object by default. You have to use the *var* keyword to have them defined locally.
+
+
+Functions passed as parameters
+------------------------------
+
+Everything is an object in Python. In particular, a function is an object. This
+means that you can assign a function to a variable or pass it as a function
+argument. This may be useful when you want to have some generic algorithms.
+This is used in the *sort* method so that you can, for example, sort strings in
+case-insensitive way:
+
+.. code-block:: python
+
+    def lowercase(s):
+        return s.lower()
+
+    my_list = ['a', 'B', 'c']
+    my_list.sort(key=lowercase)
+    print my_list
+
+You can pass functions as parameters and assign them to variables in JavaScript
+too, although there is one important detail that is different about methods.
+
+Method binding
+--------------
+
+Methods in Python are bound to object instances. This means that you can pass
+them around as shown above, and they will still remember what object they
+belong to.
+
+.. code-block:: python
+
+    def call_with_a(method):
+        method('a')
+
+    my_list = []
+    call_with_a(my_list.append)
+    print my_list
+
+This is not true for JavaScript. In JavaScript the functions and methods don't
+know where they came from, and they get the reference to their object at the
+moment when they are called.
+
+Closures
+--------
+
+You can define a function inside other function in Python. That new function
+then has access to all the variables in the local scope of the initial
+function. Moreover, if you return that new function from the original function,
+all the variables in the original function's scope are still alive -- because
+the new function still has access to them. This saved scope is called a
+closure.
+
+An example use of a closure is a "private" variable that can be only accessed
+by *get* and *set* functions. 
+
+.. code-block:: python
+
+    def create_get_set():
+        """
+        Get a pair of functions for getting and setting a hidden value.
+        Using a closure.
+        """
+
+        value = None
+
+        def get():
+            return value
+
+        def set(new_value):
+            value = new_value
+
+        return get, set
+
+The *value* variable stays in the memory even after *create_get_set* finishes,
+because the *get* and *set* functions still hold a reference to it. Because
+they are the only objects holding reference to it, the variable is effectively
+hidden from other code. You could have a similar effect by using an object:
+
+.. code-block:: python
+
+    class GetSet(object):
+        def __init__(self):
+            self.value = None
+
+        def get(self):
+            return self.value
+
+        def set(self, new_value):
+            self.value = new_value
+
+    def create_get_set():
+        """
+        Get a pair of functions for getting and setting a hidden value.
+        Using a closure.
+        """
+
+        getset = GetSet()
+        return getset.get, getset.set
+
+
+Here instead of a closure we have an explicit *getset* object. Again, this
+object stays in memory because the bound methods *get* and *set* hold a
+reference to it. This couldn't be done in JavaScript.
+
+
+Star arguments
+--------------
+
+You can define a function that takes a variable number of arguments:
+
+.. code-block:: python
+
+    def average(*args):
+        total = 0
+        for arg in args:
+            total += arg
+        return float(total)/len(args)
+
+And you can even have a function that takes keyword arguments:
+
+.. code-block:: python
+
+    def get_arguments(*args, **kwargs):
+        return args, kwargs
+
+    get_argumenst(1, 2, 3, four=5)
+
+You can also call a function and pass it a dictionary or list as the argument:
+
+.. code-block:: python
+
+    my_list = [1, 2, 3]
+    my_dict = {'four': 5}
+
+    get_arguments(*my_list, **my_dict)
+
+In JavaScript there are other mechanisms for doing that: the *arguments* keyword
+has the list of all parameters passed to a function. These are all positional arguments, JavaScript doesn't have keyword arguments. The *apply* method of a function lets you pass an array of parameters in a function call.
+

tex/easiest-quiz-ever.tex

+\documentclass{beamer}
+
+\usepackage{pgfpages}
+\usepackage{rotating}
+%\pgfpagesuselayout{2 on 1}[a4paper,border shrink=5mm]
+
+\usepackage{graphicx}
+%\usetheme{Madrid}
+
+
+
+\def\resourceframe{%
+  \begin{frame}
+    \frametitle{Explanations and information}
+
+    See JavaScript on the conference wiki, which is probably
+    
+    \bigskip
+    http://wiki.europython.eu/JavaScript.
+  \end{frame}
+}
+
+\title{The easiest quiz in the world \\ ever}
+\author{Jonathan Fine, Open University, UK}
+\date[]{EuroPython Lightning talk, 2010\\ 
+
+}
+
+\begin{document}
+
+\begin{frame}
+\maketitle
+\end{frame}
+
+\begin{frame}
+\frametitle{Question 1: Panama hats}
+
+Q:  In which country were Panama hats originally made?
+
+\bigskip
+\bigskip
+\bigskip
+\pause
+A: Ecuador.  
+
+\bigskip
+Panama was a city in Ecuador before the creation of the Panama Canal
+Zone and then the country Panama.
+\end{frame}
+
+\begin{frame}[fragile]
+\frametitle{Question 2: Equality is transitive}
+
+Command line SpiderMonkey JavaScript interpreter:
+
+\begin{verbatim}
+easiest-quiz-ever$ js
+js> a = '0'
+js> b = 0
+js> c = ''
+
+js> a == b
+true
+js> b == c
+true
+js> a == c      // Question.
+\end{verbatim}
+
+\bigskip
+\pause
+\begin{verbatim}
+js> a == c
+false
+\end{verbatim}
+
+\end{frame}
+
+\resourceframe
+
+\begin{frame}
+\frametitle{Question 3: Canary Islands}
+
+Q:  After which creature were the Canary Islands named?
+
+\bigskip
+\bigskip
+\bigskip
+\pause 
+
+A: Dog (canine).  
+
+\bigskip
+As I recall, there were many dog seals on one the
+Canary Islands.
+\end{frame}
+
+\begin{frame}[fragile]
+\frametitle{Question 4:  Assignment assigns}
+
+\begin{verbatim}
+js> words = 'Call me Ishmael.'
+Call me Ishmael.
+js> words.lang = 'en'
+js> lang = words.lang       // What is lang?
+\end{verbatim}
+
+\bigskip
+\pause
+\begin{verbatim}
+js> lang == undefined
+true
+\end{verbatim}
+
+\pause
+\begin{verbatim}
+js> lang = words.lang = 'en'  // What is lang?
+\end{verbatim}
+
+\pause
+\begin{verbatim}
+js> lang
+en
+\end{verbatim}
+
+\end{frame}
+
+\begin{frame}
+\frametitle{Question 5: King George}
+
+Q:  What was the first name of Britain's King George VI?
+
+\bigskip
+\bigskip
+\bigskip
+\pause
+
+A: Albert.  
+
+\bigskip
+He had several names, and Queen Victoria said she wanted
+her beloved Albert to be the only English monarch of that name.
+
+\end{frame}
+
+\begin{frame}[fragile]
+\frametitle{Question 6: Dot binds}
+
+\begin{verbatim}
+js> a = [0, 1, 2]
+0,1,2
+js> a.push(3); a.push(4);
+js> a
+0,1,2,3,4
+\end{verbatim}
+
+\pause
+\begin{verbatim}
+js> b = [0, 1, 2]
+js> tmp = b.push
+js> tmp(3); tmp(4);   // What is 'b'?
+\end{verbatim}
+
+\pause
+\begin{verbatim}
+js> b
+0,1,2
+\end{verbatim}
+
+\pause
+\begin{verbatim}
+js> [length, this[0], this[1]]
+2,3,4
+\end{verbatim}
+\end{frame}
+
+\begin{frame}
+\frametitle{Question 7: White rhinocerous}
+
+Q:  What is the colour of a white rhinocerous?
+
+\bigskip
+\bigskip
+\bigskip
+\pause
+A:  Grey.  
+
+\bigskip
+The name is a corruption of the Dutch work for `wide'.
+\end{frame}
+
+
+\begin{frame}[fragile]
+\frametitle{Question 8: False is false}
+
+\begin{verbatim}
+js> true ? 1 : 0
+1
+js> false ? 1 : 0
+0
+\end{verbatim}
+
+\bigskip
+\pause
+\begin{verbatim}
+js> new Boolean(false) ? 1 : 0  // Question
+\end{verbatim}
+\pause
+\begin{verbatim}
+1
+\end{verbatim}
+
+\bigskip
+\pause
+\begin{verbatim}
+js> Boolean(false) ? 1 : 0
+\end{verbatim}
+\pause
+\begin{verbatim}
+0
+\end{verbatim}
+\end{frame}
+
+\resourceframe
+
+\begin{frame}
+\frametitle{Question 9: Black box}
+
+Q:  What colour is the black box flight recorder carried by commercial planes?
+
+\bigskip
+\bigskip
+\bigskip
+\pause
+A:  Orange.
+
+\bigskip
+
+This makes it more visible.
+\end{frame}
+
+\begin{frame}
+\frametitle{Question 10: Like JavaScript}
+
+Q: JavaScript is not like which programming language \ldots
+
+\pause 
+\bigskip
+\ldots created by Sun?
+
+\bigskip
+\bigskip
+\bigskip
+\pause
+A:  Java.
+\end{frame}
+
+
+\begin{frame}
+\frametitle{Thank you}
+
+\bigskip
+
+Don't forget JavaScript on the conference wiki for more
+information.
+
+\end{frame}
+
+\end{document}