Difference between revisions of "Python"

From MiniScript Wiki
Jump to navigation Jump to search
Line 63: Line 63:
 
| 69
 
| 69
 
| 53
 
| 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 <c>if</c>/<c>end if</c>, <c>for</c>/<c>end for</c>, and <c>function</c>/<c>end function</c> (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.
 +
 +
{| class="wikitable"
 +
|-
 +
! Python
 +
! MiniScript
 +
|-
 +
| <pre>def myfunc():
 +
  for i in range(1, 11):
 +
    if i == 9:
 +
      print("Almost done...")
 +
    else:
 +
      print(i)
 +
 +
myfunc()</pre>
 +
| <ms>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</ms>
 
|}
 
|}
  
Line 80: Line 110:
 
print x</ms>
 
print x</ms>
 
|}
 
|}
 
=== Block Syntax ===
 
  
 
=== Magic Methods ===
 
=== Magic Methods ===

Revision as of 21:17, 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.

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 (aka float) data type. Note that in MiniScript, False and True are represented directly by the numbers 0 and 1; there is no separate bool 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

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