Some Notes for Python UsersFebruary 03. 2012
Some differences between Wirbel and PythonWirbel's syntax and semantics are as close as possible and desirable to Python. For various reasons there are some differences, however. This article explains some of them. In Python print is a builtin command. In Wirbel it is just an ordinary function call. That means for you: Use paranthesis! In Python: Python
print "Hello World" Same in Wirbel: Wirbel
print("Hello World")
True, FalseAll reserved keywords in Wirbel are lower case. Such are true and false. True and False do not exist (though can define them if you want). By the way: true is not 1 and false is not 0 in Wirbel. Boolean and Integer are two distinct data types. Characters and StringsJust as C Wirbel provides a special data type for a single Ascii character. That is essentially one byte and implemented as C char. A character literal is written within single quotes, string literals in double quotes: a = "Hello" # a has type string b = a[1] # b has type char print(b == 'e') # --> true When converting Python programs to Wirbel, replace you single-quoted strings with double quotes. NoneWirbel does not have a None value comparable to that in Python. If a function does not return anything, it - well - does not return anything, not even None. But Wirbel provides the keyword null, which is a value that all complex data types can have. That way you can distinguish an empty list from a null value. Use the is operator to check, if an object is null: a = null print(a is null) # true print(a.len()) # exception value.null a = [] print(a is null) # false print(a.len()) # 0 TypesThe key difference between Wirbel and Python lies in the Type concept. In Python variables do not have types - only values have. In Wirbel everything is typed, much like in C and Java. But fortunately you never have to declare types. Wirbel infers types at compile time and automatically assigns a proper type to each variable and expression. You just have to make sure that this is possible. You do this by avoiding two kinds of typing errors: Type conflictsConsider the following code: a = 17 a = "Seventeen" This is correct Python code. But in Wirbel you get a compile error. The variable a cannot at the same time be of type integer and string. Incomplete type informationSometimes Wirbel cannot determine the type of an expression because of lack of information. Here is an example: a = [1, 2, 3] b = [] # Problem: type of b unknown a clearly has the type "list of integer". But what about b? It is a list - but of what? Wirbel cannot know an will fail to compile. But in a real program you would certainly do something with b, for example: a = [1, 2, 3] b = [] print(a + b) # clarifies type of b Now Wirbel knows that also b must be a list of integers and everything is fine. In other words: eliminate unused variables that are initialized with [], {} or null. ListsLists in Python can contain elements with mixed types: Python
alist = [ 1, 0.5, None, "Hello" ] # valid in Python In Wirbel lists must contain elements of the same type: Wirbel ilist = [ 1, 18, -500 ] # list of int flist = [ 0.5, 17.3 ] # list of float listlist = [ [true, false], [], [false] ] # list of list of boolean This enforces a bit more programming discipline here and there but has the advantage that lists can be implemented as C-arrays of C datatypes. A list of 1 million integers needs roughly 4 million bytes in Wirbel - just as in C. Furthermore operating with lists is type safe. Type inconsistencies are detected at compile time. Code that uses a list does not need to check the elements types. TuplesOther than in Python [tuples] are mutable in Wirbel. Just as with lists you can change components values. In Wirbel the following is legal: a = (1, true, "Hi") a[1] = false # a is now (1, false, "Hi") As you see from the example tuples can have mixed types. Tuples are implemented as C structs and the very efficient. All tuples types have to be known at compile time. It is not possible to create a tuple from a list at runtime: Python
a = [ 1, 4, 5 ] b = tuple(a) # illegal in Wirbel DictionariesWith dict its the same as with list. Both the kesy and the values must have uniform types. Of course key and value types can be different from each other. The current implementation of dicts in Wirbel uses trees. That has the advantage over hashes that they are always sorted according to the keys at the price of being somewhat slower. Furthermore any datatype can be used as keys - even lists and other dicts. If you use mutable objects as keys (such as lists) then you have to make sure for yourself that they never changed while in usage as key in a dictionary. Function overloadingIn Wirbel you may define an arbitrary number of functions with the same name. When handling a call to that function name Wirbel will try to select the correct version depending on the number and type of arguments. All you have to do is to make sure that in any case exactly one version is matching. The following is correct Wirbel code:
def my_sum(a, b):
return a + b
def my_sum(a, b, c):
return a + b + c
def my_sum(alist):
s = 0
for item in alist: s += item
return s
print(my_sum(4, 5)) # --> 9
print(my_sum(1, 2, 3)) # --> 6
print(my_sum([1, 2, 3, 4])) # --> 10
importWirbel does not know an import statement. That is because it is not needed. Just leave it out. Wirbel will find everything it needs. |
| |||||||||||||||||||||