Rock It 《ML》JupyterLab 【丁】Code《七》語義【二】補篇

認識一棵『樹』

Tree (data structure)

A simple unordered tree; in this diagram, the node labeled 7 has two children, labeled 2 and 6, and one parent, labeled 2. The root node, at the top, has no parent.

In computer science, a tree is a widely used abstract data type (ADT)—or data structure implementing this ADT—that simulates a hierarchical tree structure, with a root value and subtrees of children with a parent node, represented as a set of linked nodes.

A tree data structure can be defined recursively as a collection of nodes (starting at a root node), where each node is a data structure consisting of a value, together with a list of references to nodes (the “children”), with the constraints that no reference is duplicated, and none points to the root.

Alternatively, a tree can be defined abstractly as a whole (globally) as an ordered tree, with a value assigned to each node. Both these perspectives are useful: while a tree can be analyzed mathematically as a whole, when actually represented as a data structure it is usually represented and worked with separately by node (rather than as a set of nodes and an adjacency list of edges between nodes, as one may represent a digraph, for instance). For example, looking at a tree as a whole, one can talk about “the parent node” of a given node, but in general as a data structure a given node only contains the list of its children, but does not contain a reference to its parent (if any).

 

始於知道『節點』是什麼,與之相逢︰

Meet the Nodes

An AST represents each element in your code as an object. These are instances of the various subclasses of AST described below. For instance, the code a + 1 is a BinOp, with a Name on the left, a Num on the right, and an Add operator.

………

固可藉圖示,一目了然!

 

仍需知其內封裝的『數據』乎?

 

不過這個 ast.dump 之輸出,不易讀也☻

所以 Green Tree Snakes 才選用 parseprint 漂亮列印吧☺

classJoinedStr(values)

New in version 3.6.

An f-string, comprising a series of FormattedValue and Str nodes.

>>> parseprint('f"sin({a}) is {sin(a):.3}"')
Module(body=[
    Expr(value=JoinedStr(values=[
        Str(s='sin('),
        FormattedValue(value=Name(id='a', ctx=Load()), conversion=-1, format_spec=None),
        Str(s=') is '),
        FormattedValue(value=Call(func=Name(id='sin', ctx=Load()), args=[
            Name(id='a', ctx=Load()),
          ], keywords=[]), conversion=-1, format_spec=JoinedStr(values=[
            Str(s='.3'),
          ])),
      ])),
  ])

Note

The pretty-printer used in these examples is available in the source repository for Green Tree Snakes.

※ 註︰

 
examples
40 B
 
5.6 KB
 
173 B
 
3.3 KB
 
173 B
 
8.0 KB
 
4.0 KB
 
1.4 KB
 
5.1 KB
 
3.5 KB
 
30.5 KB
 
2.8 KB
 
This is the source repository for Green Tree Snakes, a field guide to Abstract Syntax Trees in Python.

 

讀過原始碼後,心想既未打包成套件,何不介紹直用之法哩︰

【依樣葫蘆】

 

【直接引用】

 

順道講點 IPython 『魔術』奧秘︰

Defining custom magics

There are two main ways to define your own magic functions: from standalone functions and by inheriting from a base class provided by IPython: IPython.core.magic.Magics. Below we show code you can place in a file that you load from your configuration, such as any file in the startupsubdirectory of your default IPython profile.

First, let us see the simplest case. The following shows how to create a line magic, a cell one and one that works in both modes, using just plain functions:

from IPython.core.magic import (register_line_magic, register_cell_magic,
                                register_line_cell_magic)

@register_line_magic
def lmagic(line):
    "my line magic"
    return line

@register_cell_magic
def cmagic(line, cell):
    "my cell magic"
    return line, cell

@register_line_cell_magic
def lcmagic(line, cell=None):
    "Magic that works both as %lcmagic and as %%lcmagic"
    if cell is None:
        print("Called as line magic")
        return line
    else:
        print("Called as cell magic")
        return line, cell

# In an interactive session, we need to delete these to avoid
# name conflicts for automagic to work on line magics.
del lmagic, lcmagic

※ 出自︰

1.4. Creating an IPython extension with custom magic commands

IPython Cookbook, Second EditionThis is one of the 100+ free recipes of the IPython Cookbook, Second Edition, by Cyrille Rossant, a guide to numerical computing and data science in the Jupyter Notebook. The ebook and printed book are available for purchase at Packt Publishing.

▶  Text on GitHub with a CC-BY-NC-ND license
▶  Code on GitHub with a MIT license

▶  Go to Chapter 1 : A Tour of Interactive Computing with Jupyter and IPython
▶  Get the Jupyter notebook

Although IPython comes with a wide variety of magic commands, there are cases where we need to implement custom functionality in new magic commands. In this recipe, we will show how to create line and magic cells, and how to integrate them in an IPython extension.

 

以饗讀者呦☆

 

 

 

 

 

 

 

 

Rock It 《ML》JupyterLab 【丁】Code《七》語義【二】

望文生義的人,閱讀

32.2. astAbstract Syntax Trees

Source code: Lib/ast.py


The ast module helps Python applications to process trees of the Python abstract syntax grammar. The abstract syntax itself might change with each Python release; this module helps to find out programmatically what the current grammar looks like.

An abstract syntax tree can be generated by passing ast.PyCF_ONLY_AST as a flag to the compile() built-in function, or using the parse() helper provided in this module. The result will be a tree of objects whose classes all inherit from ast.AST. An abstract syntax tree can be compiled into a Python code object using the built-in compile() function.

 

文本,大概不會追問『ASDL』是什麼吧!

32.2.2. Abstract Grammar

The abstract grammar is currently defined as follows:

-- ASDL's six builtin types are identifier, int, string, bytes, object, singleton

module Python
{
    mod = Module(stmt* body)
        | Interactive(stmt* body)
        | Expression(expr body)

        -- not really an actual node but useful in Jython's typesystem.
        | Suite(stmt* body)

    stmt = FunctionDef(identifier name, arguments args,
                       stmt* body, expr* decorator_list, expr? returns)
          | AsyncFunctionDef(identifier name, arguments args,
                             stmt* body, expr* decorator_list, expr? returns)

          | ClassDef(identifier name,
             expr* bases,
             keyword* keywords,
             stmt* body,
             expr* decorator_list)
          | Return(expr? value)

          | Delete(expr* targets)
          | Assign(expr* targets, expr value)
          | AugAssign(expr target, operator op, expr value)

          -- use 'orelse' because else is a keyword in target languages
          | For(expr target, expr iter, stmt* body, stmt* orelse)
          | AsyncFor(expr target, expr iter, stmt* body, stmt* orelse)
          | While(expr test, stmt* body, stmt* orelse)
          | If(expr test, stmt* body, stmt* orelse)
          | With(withitem* items, stmt* body)
          | AsyncWith(withitem* items, stmt* body)

          | Raise(expr? exc, expr? cause)
          | Try(stmt* body, excepthandler* handlers, stmt* orelse, stmt* finalbody)
          | Assert(expr test, expr? msg)

          | Import(alias* names)
          | ImportFrom(identifier? module, alias* names, int? level)

          | Global(identifier* names)
          | Nonlocal(identifier* names)
          | Expr(expr value)
          | Pass | Break | Continue

          -- XXX Jython will be different
          -- col_offset is the byte offset in the utf8 string the parser uses
          attributes (int lineno, int col_offset)

          -- BoolOp() can use left & right?
    expr = BoolOp(boolop op, expr* values)
         | BinOp(expr left, operator op, expr right)
         | UnaryOp(unaryop op, expr operand)
         | Lambda(arguments args, expr body)
         | IfExp(expr test, expr body, expr orelse)
         | Dict(expr* keys, expr* values)
         | Set(expr* elts)
         | ListComp(expr elt, comprehension* generators)
         | SetComp(expr elt, comprehension* generators)
         | DictComp(expr key, expr value, comprehension* generators)
         | GeneratorExp(expr elt, comprehension* generators)
         -- the grammar constrains where yield expressions can occur
         | Await(expr value)
         | Yield(expr? value)
         | YieldFrom(expr value)
         -- need sequences for compare to distinguish between
         -- x < 4 < 3 and (x < 4) < 3
         | Compare(expr left, cmpop* ops, expr* comparators)
         | Call(expr func, expr* args, keyword* keywords)
         | Num(object n) -- a number as a PyObject.
         | Str(string s) -- need to specify raw, unicode, etc?
         | Bytes(bytes s)
         | NameConstant(singleton value)
         | Ellipsis

         -- the following expression can appear in assignment context
         | Attribute(expr value, identifier attr, expr_context ctx)
         | Subscript(expr value, slice slice, expr_context ctx)
         | Starred(expr value, expr_context ctx)
         | Name(identifier id, expr_context ctx)
         | List(expr* elts, expr_context ctx)
         | Tuple(expr* elts, expr_context ctx)

          -- col_offset is the byte offset in the utf8 string the parser uses
          attributes (int lineno, int col_offset)

    expr_context = Load | Store | Del | AugLoad | AugStore | Param

    slice = Slice(expr? lower, expr? upper, expr? step)
          | ExtSlice(slice* dims)
          | Index(expr value)

    boolop = And | Or

    operator = Add | Sub | Mult | MatMult | Div | Mod | Pow | LShift
                 | RShift | BitOr | BitXor | BitAnd | FloorDiv

    unaryop = Invert | Not | UAdd | USub

    cmpop = Eq | NotEq | Lt | LtE | Gt | GtE | Is | IsNot | In | NotIn

    comprehension = (expr target, expr iter, expr* ifs)

    excepthandler = ExceptHandler(expr? type, identifier? name, stmt* body)
                    attributes (int lineno, int col_offset)

    arguments = (arg* args, arg? vararg, arg* kwonlyargs, expr* kw_defaults,
                 arg? kwarg, expr* defaults)

    arg = (identifier arg, expr? annotation)
           attributes (int lineno, int col_offset)

    -- keyword arguments supplied to call (NULL identifier for **kwargs)
    keyword = (identifier? arg, expr value)

    -- import name with optional 'as' alias.
    alias = (identifier name, identifier? asname)

    withitem = (expr context_expr, expr? optional_vars)
}

 

如果不知道是哪種『語法樹』︰

25. Design of CPython’s Compiler

25.1. Abstract

In CPython, the compilation from source code to bytecode involves several steps:

  1. Parse source code into a parse tree (Parser/pgen.c)
  2. Transform parse tree into an Abstract Syntax Tree (Python/ast.c)
  3. Transform AST into a Control Flow Graph (Python/compile.c)
  4. Emit bytecode based on the Control Flow Graph (Python/compile.c)

The purpose of this document is to outline how these steps of the process work.

This document does not touch on how parsing works beyond what is needed to explain what is needed for compilation. It is also not exhaustive in terms of the how the entire system works. You will most likely need to read some source to have an exact understanding of all details.

 

莫非香蕉、芭樂風味一樣乎?

25.3. Abstract Syntax Trees (AST)

The abstract syntax tree (AST) is a high-level representation of the program structure without the necessity of containing the source code; it can be thought of as an abstract representation of the source code. The specification of the AST nodes is specified using the Zephyr Abstract Syntax Definition Language (ASDL)[Wang97].

The definition of the AST nodes for Python is found in the file Parser/Python.asdl.

Each AST node (representing statements, expressions, and several specialized types, like list comprehensions and exception handlers) is defined by the ASDL. Most definitions in the AST correspond to a particular source construct, such as an ‘if’ statement or an attribute lookup. The definition is independent of its realization in any particular programming language.

 

所以前行者,最好先了解一下

Zephyr ASDL

※ 註︰

Zephyr ASDL Home Page

ASDL Logo

Home of ASDL

I’m in the process of migrating the ASDL webpages from the old site. For a current version of ASDL checkout the CVS repository.

Introduction

The Zephyr Abstract Syntax Description Lanuguage (ASDL) is a language designed to describe the tree-like data structures in compilers. Its main goal is to provide a method for compiler components written in different languages to interoperate. ASDL makes it easier for applications written in a variety of programming languages to communicate complex recursive data structures.

asdlGen is a tool that takes ASDL descriptions and produces implementations of those descriptions in a variety of popular languages. ASDL and asdlGen together provide the following advantages

  • Concise descriptions of important data structures.
  • Automatic generation of data structure implementations for C, C++, Java, Standard ML, and Haskell.
  • Automatic generation of functions to read and write the data structures to disk in a machine and language independent way.

ASDL descriptions describe the tree-like data structures such as abstract syntax trees (ASTs) and compiler intermediate representations (IRs). Tools such as asdlGen automatically produce the equivalent data structure definitions for C, C++, Java, Standard ML, OCaml, and Haskell. asdlGen also produces functions for each language that read and write the data structures to and from a platform and language independent sequence of bytes. The sequence of bytes is called a pickle.

ASDL pickles can be interactively viewed and edited with a graphical browser, or pretty printed into a simple textual format. The browser provides some advanced features such as display styles and tree based versions of standard unix tools such as diff and grep. ASDL was part of the Zephyr National Compiler Infrastructure project.

Documentation, bugs, software etc….

See the Source Forge project page.

 

再借著

Green Tree Snakes – the missing Python AST docs

Abstract Syntax Trees, ASTs, are a powerful feature of Python. You can write programs that inspect and modify Python code, after the syntax has been parsed, but before it gets compiled to byte code. That opens up a world of possibilities for introspection, testing, and mischief.

The official documentation for the ast module is good, but somewhat brief. Green Tree Snakes is more like a field guide (or should that be forest guide?) for working with ASTs. To contribute to the guide, see the source repository.

Contents:

 

入 AST 大門也☆

 

 

 

 

 

 

 

Rock It 《ML》JupyterLab 【丁】Code《七》語義【一】

然後大膽『解讀』《 TPG 》 Toy Parser Generator 『原始碼』,『遊藝』於『文法』『語意』物件之中,『採擷』 TPG 之『自生自成』的『精華』︰

Toy Parser Generator is a lexical and syntactic parser generator for Python. This generator was born from a simple statement: YACC is too complex to use in simple cases (calculators, configuration files, small programming languages, …).

TPG can very simply write parsers that are usefull for most every day needs (even if it can’t make your coffee). With a very clear and simple syntax, you can write an attributed grammar that is translated into a recursive descendant parser. TPG generated code is very closed to the original grammar. This means that the parser works like the grammar. A grammar rule can be seen as a method of the parser class, symbols as method calls, attributes as method parameters and semantic values as return values. You can also add Python code directly into grammar rules and build abstract syntax trees while parsing.

The first application of TPG is TPG itself. The first (not released) version of TPG has been written by hand then was used to generate next versions. Now TPG can generate itself.

For an uptodate documentation, please read tpg.pdf.

─── 《W!O 的派生‧十日談之《九》

 

所謂四則運算『先乘除後加減』是說︰

2 \ + \ 3 \times 5 \ = 2 \ + \ 15

用『括號』() ── 運算優先權 ── 解釋為︰

2 \ + \ 3 \times 5 \ = 2 \ + \ (\ 3 \times 5)

故其『語意』不會是︰

2 \ + \ 3 \times 5 \ = (2 \ + \ 3) \times 5 \ = \ 25 也。

派生 Python 會不順應『數學傳統』乎?想要目睹耶?

 

實則『語義』通常孕育於『語法結構』中呦☆

抽象語法樹

下列輾轉相除法編程碼的抽象語法樹:

while b ≠ 0

if a > b

a := a − b
else

b := b − a
return a

計算機科學中,抽象語法樹Abstract Syntax Tree,AST),或簡稱語法樹(Syntax tree),是原始碼語法結構的一種抽象表示。它以樹狀的形式表現程式語言的語法結構,樹上的每個節點都表示原始碼中的一種結構。之所以說語法是「抽象」的,是因為這裡的語法並不會表示出真實語法中出現的每個細節。比如,嵌套括號被隱含在樹的結構中,並沒有以節點的形式呈現;而類似於 if-condition-then 這樣的條件跳轉語句,可以使用帶有兩個分支的節點來表示。

和抽象語法樹相對的是具體語法樹(通常稱作分析樹)。一般的,在原始碼的翻譯和編譯過程中,語法分析器創建出分析樹,然後從分析樹生成AST。一旦AST被創建出來,在後續的處理過程中,比如語義分析階段,會添加一些信息。

※ 註︰

sudo pip3 install showast

/show_ast

An IPython notebook plugin for visualizing ASTs.

showast

PyPI version Liberapay receiving

An IPython/Jupyter notebook plugin for visualizing abstract syntax trees.

 

若言連『計算機語言』之『語義學』都尚未得到定論︰

Semantics (computer science)

In programming language theory, semantics is the field concerned with the rigorous mathematical study of the meaning of programming languages. It does so by evaluating the meaning of syntactically valid strings defined by a specific programming language, showing the computation involved. In such a case that the evaluation would be of syntactically invalid strings, the result would be non-computation. Semantics describes the processes a computer follows when executing a program in that specific language. This can be shown by describing the relationship between the input and output of a program, or an explanation of how the program will be executed on a certain platform, hence creating a model of computation.

Formal semantics, for instance, helps to write compilers, better understand what a program is doing, and to prove, e.g., that the following if statement

if 1 == 1 then S1 else S2

has the same effect as S1 alone.

Overview

The field of formal semantics encompasses all of the following:

  • The definition of semantic models
  • The relations between different semantic models
  • The relations between different approaches to meaning
  • The relation between computation and the underlying mathematical structures from fields such as logic, set theory, model theory, category theory, etc.

It has close links with other areas of computer science such as programming language design, type theory, compilers and interpreters, program verification and model checking.

……

Describing relationships

For a variety of reasons, one might wish to describe the relationships between different formal semantics. For example:

  • To prove that a particular operational semantics for a language satisfies the logical formulas of an axiomatic semantics for that language. Such a proof demonstrates that it is “sound” to reason about a particular (operational) interpretation strategy using a particular (axiomatic) proof system.
  • To prove that operational semantics over a high-level machine is related by a simulation with the semantics over a low-level machine, whereby the low-level abstract machine contains more primitive operations than the high-level abstract machine definition of a given language. Such a proof demonstrates that the low-level machine “faithfully implements” the high-level machine.

It is also possible to relate multiple semantics through abstractions via the theory of abstract interpretation.

………

 

故宜暫放下未來

自然後設語義

自然後設語義 (Natural Semantic Metalanguage, NSM),又稱自然語義後設語言,或是簡稱自然語義理論,是語言學當中語義學的理論之一,源起於波蘭語言學者安德列傑‧波古斯洛斯基(Andrzej Bogusławski)的概念。主要理論創建者是澳洲籍波蘭語言學家安娜 ‧維茲畢卡(Anna Wierzbicka),創於70年代早期(Wierzbicka 1972), 其先在華沙大學,後來以澳洲國立大學為學派的大本營。90年代後又與澳洲學者克里福‧高得(Cliff Goddard)繼續合作拓展研究領域和方向(Goddard & Wierzbicka 1994, 2002)。

理論

自然後設語義學派著重於使用有限、簡單、普遍的詞彙概念作為語義元(semantic primitives或semantic primes),運用來把較為複雜的語義概念分析到最簡單的概念,此種語義分析稱為簡約釋意(reductive paraphrase)。自然後設語義廣泛研究語言與認知、語言與文化等議題。主要的研究領域包括詞彙語義、語法語義、措辭法 、語用學,以及跨文化溝通等。這個理論強調語義學研究應該針對語義本身的描述,而非結構學派重視的語義關係或是形式語義學派重視的命題真假值。語義的描述要能夠給出簡單、清楚且無循環的定義,使用的後設語言應該是自然語言當中的語義元詞,反對透過抽象邏輯式的後設語言來呈現,因為抽象的後設語言也還是需要透過自然語言去了解其定義。

目前使用這個理論研究過的語言包括英語俄語波蘭語法語德語西班牙語瑞典語等歐洲語言,還有馬來語(屬於南島語族)、日本語 韓語標準漢語、亞威語(Ewe)(屬於非洲語言)、東克雷語(East Cree)(屬於美洲語言)和楊固尼加加拉語(Yankunytjatjara)(屬於澳洲語言)等等。[1] [2]

 

…爭議,先多求認識言語為何而用哩◎

 

 

 

 

 

 

 

Rock It 《ML》JupyterLab 【丁】Code《六》後

如果說派生之『標識符』未綁定『物件』前,不能存在、沒有意指 ,那麼在『函式』定義時,只能是『形式參數』 formal parameter乎?因為所指『物件』是什麼『東東』未知也!所以我們能寫如下合乎『語法』的『兩倍』 function 哩!!

 

然則『兩倍』不就可『加倍』嗎??一時打字之不小心,恐怕必須要『除錯』呦☻

 

因此早已有

27.3. pdbThe Python Debugger

Source code: Lib/pdb.py


The module pdb defines an interactive source code debugger for Python programs. It supports setting (conditional) breakpoints and single stepping at the source line level, inspection of stack frames, source code listing, and evaluation of arbitrary Python code in the context of any stack frame. It also supports post-mortem debugging and can be called under program control.

The debugger is extensible – it is actually defined as the class Pdb. This is currently undocumented but easily understood by reading the source. The extension interface uses the modules bdb and cmd.

The debugger’s prompt is (Pdb). Typical usage to run a program under control of the debugger is:

>>> import pdb
>>> import mymodule
>>> pdb.run('mymodule.test()')
> <string>(0)?()
(Pdb) continue
> <string>(1)?()
(Pdb) continue
NameError: 'spam'
> <string>(1)?()
(Pdb)

Changed in version 3.3: Tab-completion via the readline module is available for commands and command arguments, e.g. the current global and local names are offered as arguments of the p command.

耶?讀者最好先知道其『用法』也。

為何呢?由於少有『說明』文件,通常不過借其『擴張』功能;

務須知 h(elp)) 以及 ?(cmd) 簡潔字句也☺

 

特介紹

/ipdb

Integration of IPython pdb

IPython pdb

https://travis-ci.org/gotcha/ipdb.png?branch=master

Use

ipdb exports functions to access the IPython debugger, which features tab completion, syntax highlighting, better tracebacks, better introspection with the same interface as the pdb module.

Example usage:

import ipdb
ipdb.set_trace()
ipdb.set_trace(context=5)  # will show five lines of code
                           # instead of the default three lines
ipdb.pm()
ipdb.run('x[0] = 3')
result = ipdb.runcall(function, arg0, arg1, kwarg='foo')
result = ipdb.runeval('f(1,2) - 3')

 

強調與 Jupyter Lab 介面之相容性哩︰

 

就像 JupyterLab 不可用

PixieDebugger – A Visual Python Debugger for Jupyter Notebooks Every Data Scientist Should Use

Overview

  • PixieDebugger is a powerful visual debugger for Python that works in Jupyter Notebooks
  • It comes with multiple features, including a source editor and a code execution controlling toolbar
  • It works as a magic command and requires PixieDust as a prerequisite

Introduction

Picture this – you think you’ve designed a phenomenal model using tons of lines of code but there’s a pesky error in there. It’s holding back the model from achieving it’s full power but you can’t quite figure out what’s wrong. Sure, you could use Jupyter’s pdb but wouldn’t it be awesome to visually debug that code block within an interactive environment? I’ve got some awesome news for you!

PixieDebugger, developed by the PixieDust team, claims to be the first visual debugger for Python that works exclusively with Jupyter Notebooks. Note that it doesn’t yet work with JupyterLab but the developers might be working on fixing this soon. The PixieDebugger comes packaged with multiple features, including:

  • A source editor
  • Console output feature
  • Local variable inspector
  • Breakpoint management
  • Code execution controlling toolbar

 

但『筆記本』可用勒☆

 

 

 

 

 

 

 

Rock It 《ML》JupyterLab 【丁】Code《六》前

學過數學的人是否知道四則運算 a \times b 有兩種『解釋』的呢?可以說 ab 個?還是說 ab 耶?然而不論 a=0b=0a \times b 都是『零』 0 也。所以任何數除以『零』 ,都是『錯誤』也!即使 0/0 在微積分『極限』 limits 觀點下,也只能說成『未定式』哩!

因此派生二

rock64@rock64:~python Python 2.7.13 (default, Sep 26 2018, 18:42:22)  [GCC 6.3.0 20170516] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> 1/0 Traceback (most recent call last):   File "<stdin>", line 1, in <module> ZeroDivisionError: integer division or modulo by zero </pre>    <span style="color: #666699;">以及派生三</span> <pre class="lang:default decode:true ">rock64@rock64:~ python3
Python 3.5.3 (default, Sep 27 2018, 17:25:39) 
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 1/0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ZeroDivisionError: division by zero

 

皆產生『執行時』『錯誤』,不至認為『意義』有誤乎?!

然則為何『整數』除以『整數』卻引發『語言』變革呦!?

rock64@rock64:~python Python 2.7.13 (default, Sep 26 2018, 18:42:22)  [GCC 6.3.0 20170516] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> 1/3 0 >>> 1.0/3 0.3333333333333333 </pre>   <pre class="lang:default decode:true ">rock64@rock64:~ python3
Python 3.5.3 (default, Sep 27 2018, 17:25:39) 
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 1/3
0.3333333333333333

 

蓋因『禪味』不喜不『直覺』,又必須『小心』顧慮吧!!

所以面對『語意黑箱』時,終究不得不『除錯』ㄛ??

%debug

%debug [--breakpoint FILE:LINE] [statement [statement ...]]

Activate the interactive debugger.

This magic command support two ways of activating debugger. One is to activate debugger before executing code. This way, you can set a break point, to step through the code from the point. You can use this mode by giving statements to execute and optionally a breakpoint.

The other one is to activate debugger in post-mortem mode. You can activate this mode simply running %debug without any argument. If an exception has just occurred, this lets you inspect its stack frames interactively. Note that this will always work only on the last traceback that occurred, so you must call this quickly after an exception that you wish to inspect has fired, because if another one occurs, it clobbers the previous one.

If you want IPython to automatically do this on every exception, see the %pdb magic for more details.

Changed in version 7.3: When running code, user variables are no longer expanded, the magic line is always left unmodified.

positional arguments:
statement Code to run in debugger. You can omit this in cell
magic mode.
optional arguments:
--breakpoint <FILE:LINE>, -b <FILE:LINE>
Set break point at LINE in FILE.