Difference between revisions of "Python"
Line 40: | Line 40: | ||
=== Map Syntax === | === Map Syntax === | ||
− | + | The syntax for map literals and map indexing is the same in Python and MiniScript: | |
+ | |||
+ | <ms>m = {"one":1, "two":2, "three":3} | ||
+ | print m["two"] // 2 | ||
+ | m["pi"] = 3.14</ms> | ||
+ | |||
+ | Unlike Python, in MiniScript when a key in a map happens to also be a valid literal, it can be accessed via [[dot syntax]]. | ||
+ | |||
+ | <ms>print m.two // 2 | ||
+ | m.pi = 3.14157</ms> | ||
+ | |||
+ | MiniScript does not have a concept of "attributes" as something separate from key/value pairs in a map. | ||
=== Variable Scope === | === Variable Scope === |
Revision as of 17:36, 10 December 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
The syntax for list literals is the same in Python and MiniScript: a series of arbitrary values, separated by commas and surrounded by square brackets.
myList = ["apple", "banana", "cherry", "date"]
Index and slice syntax is also the same in both, except that MiniScript slicing supports only two parameters; it does not support the third (IndexJump) parameter.
myList[0] // "apple"
myList[:2] // ["apple", "banana"]
myList[-3:] // ["banana", "cherry", "date"]
myList[1:-1] // ["banana", "cherry"]
myList[:] // independent copy of ["apple", "banana", "cherry", "date"]
Unlike Python, you cannot assign to a slice; you can only assign to a single index.
myList[1] = "orange" // myList is now: ["apple", "orange", "cherry", "date"]
myList[1:3] = ["lemon"] // DOES NOT WORK
Map Syntax
The syntax for map literals and map indexing is the same in Python and MiniScript:
m = {"one":1, "two":2, "three":3}
print m["two"] // 2
m["pi"] = 3.14
Unlike Python, in MiniScript when a key in a map happens to also be a valid literal, it can be accessed via dot syntax.
print m.two // 2
m.pi = 3.14157
MiniScript does not have a concept of "attributes" as something separate from key/value pairs in a map.
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 |
Block Syntax
The most obvious difference between Python and MiniScript is in block syntax. While Python defines blocks by ending the opening line with a colon, and indenting the body of the block, MiniScript uses keyword pairs like if
/end if
, for
/end for
, and function
/end function
(more similar to Visual Basic). Indentation in MiniScript is strictly cosmetic, and is ignored by the compiler; and tools can automatically standardize or re-indent based on the keywords.
Python | MiniScript |
---|---|
def myfunc(): for i in range(1, 11): if i == 9: print("Almost done...") else: print(i) myfunc() |
myFunc = function
for i in range(1, 10)
if i == 9 then
print "Almost done..."
else
print i
end if
end for
end function
myFunc
|
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
|
Magic Methods
import
range function
The very common built-in function range
in both languages takes up to three arguments: start, stop, and step. But there are a few differences to note:
- In Python, the stop value is not included in the resulting sequence, while in MiniScript it is (i.e., it is an inclusive bound).
- In Python, if stop is less than start, the result is an empty list unless you also specify a negative step. In MiniScript, if step is omitted in such a case, it defaults to -1 (and you get a list that counts down).
- In Python, if only one argument is given, it is considered stop and the resulting sequence counts from 0 up to (but not including) that value. In MiniScript, a single argument is start and the resulting sequence counts from that number down (or up) to 0.
- In Python, the result of
range
is a range object; in MiniScript it is an ordinary list.