Difference between revisions of "Python"
(Created page with "Python is a popular scripting language that was first released in 1991. This article compares Python to MiniScript, and may be useful to people coming to MiniScript with a ba...") |
|||
Line 4: | Line 4: | ||
=== Data Types === | === Data Types === | ||
+ | |||
+ | MiniScript's six data types have direct analogs to common types in Python: | ||
+ | |||
+ | * '''Numbers''' in MiniScript are double-precision floating point numbers, equivalent to Python's <c>numbers.Real</c> (aka <c>float</c>) data type. Note that in MiniScript, False and True are represented directly by the numbers 0 and 1; there is no separate <c>bool</c> type. | ||
+ | |||
+ | * '''Strings''' in both languages are an immutable sequence of Unicode code points. String literals in both languages can be enclosed in double-quotes; MiniScript does not have the optional single-quote or triple-quote syntax. While Python uses backslash-escaping to include a quotation mark within a literal, in MiniScript you just write the quotation mark twice, as in SQL. | ||
+ | |||
+ | * '''Lists''' in both languages are a mutable sequence of arbitrary values; a list literal uses square brackets surrounding comma-delimited values (see List Syntax, below). Note that MiniScript does not have a <c>tuple</c> type, and uses a list in situations where Python might use a tuple. | ||
+ | |||
+ | * '''Maps''' in both languages are mutable, unordered set of key-value pairs, with the same literal syntax (see Map Syntax and Object-Oriented Programming, below). | ||
+ | |||
+ | * '''Function objects''' in both languages are similar: first-class, callable objects. However functions in MiniScript have no attributes, and the syntax for defining functions is somewhat different. | ||
+ | |||
+ | * '''<c>null</c>''' is a special datatype in both languages, representing a null reference. | ||
=== List Syntax === | === List Syntax === | ||
+ | |||
+ | (ToDo) | ||
=== Map Syntax === | === Map Syntax === | ||
+ | |||
+ | (ToDo) | ||
=== Variable Scope === | === Variable Scope === | ||
+ | |||
+ | Variables in both Python and MiniScript are local by default. There are some subtle differences, however: in Python, an inner function can implicitly access the variables of an outer function, but ''can't'' implicitly access global variables, while in MiniScript, any function can implicitly read values from both the outer context and the global context. To update a global variable, where Python uses a special <c>global</c> statement, MiniScript uses a <c>globals</c> prefix in a manner similar to using <c>self</c> to access object data. | ||
+ | |||
+ | (ToDo: example) | ||
=== Object-Oriented Programming === | === Object-Oriented Programming === | ||
+ | |||
+ | (ToDo) | ||
== Differences == | == Differences == |
Revision as of 21:09, 27 November 2022
Python is a popular scripting language that was first released in 1991. This article compares Python to MiniScript, and may be useful to people coming to MiniScript with a background in Python.
Contents
Similarities
Data Types
MiniScript's six data types have direct analogs to common types in Python:
- Numbers in MiniScript are double-precision floating point numbers, equivalent to Python's
numbers.Real
(akafloat
) data type. Note that in MiniScript, False and True are represented directly by the numbers 0 and 1; there is no separatebool
type.
- Strings in both languages are an immutable sequence of Unicode code points. String literals in both languages can be enclosed in double-quotes; MiniScript does not have the optional single-quote or triple-quote syntax. While Python uses backslash-escaping to include a quotation mark within a literal, in MiniScript you just write the quotation mark twice, as in SQL.
- Lists in both languages are a mutable sequence of arbitrary values; a list literal uses square brackets surrounding comma-delimited values (see List Syntax, below). Note that MiniScript does not have a
tuple
type, and uses a list in situations where Python might use a tuple.
- Maps in both languages are mutable, unordered set of key-value pairs, with the same literal syntax (see Map Syntax and Object-Oriented Programming, below).
- Function objects in both languages are similar: first-class, callable objects. However functions in MiniScript have no attributes, and the syntax for defining functions is somewhat different.
null
is a special datatype in both languages, representing a null reference.
List Syntax
(ToDo)
Map Syntax
(ToDo)
Variable Scope
Variables in both Python and MiniScript are local by default. There are some subtle differences, however: in Python, an inner function can implicitly access the variables of an outer function, but can't implicitly access global variables, while in MiniScript, any function can implicitly read values from both the outer context and the global context. To update a global variable, where Python uses a special global
statement, MiniScript uses a globals
prefix in a manner similar to using self
to access object data.
(ToDo: example)
Object-Oriented Programming
(ToDo)
Differences
Language Scale
MiniScript is a considerably smaller language by any reasonable measure. For example, the following table compares Python and MiniScript on C/C++ source code lines, source code files, number of data types, and number of standard intrinsic functions, as of 2021 (source).
Python | MiniScript | |
---|---|---|
Source Lines | 661,775 | 13,752 |
Source Files | 718 | 46 |
Data Types | 34 | 6 |
Intrinsics | 69 | 53 |
Function Invocation
While functions in Python are always invoked by putting parentheses after a function reference, in MiniScript, such parentheses can be omitted when (1) the function call is the entire statement, or (2) there are no arguments.
Python | MiniScript |
---|---|
import random x = random.uniform(0,1) print(x) |
x = rnd print x |