IronPython Dynamic Languages

Çarşamba, 7 Ekim 2009 07:39 by ikivanc
As you know programming languages classified in two way; Dynamic Languages and Static Languages.

In some cases Dynamic Languages has many advantages rather then Static Languages. Python/IronPython and Ruby are Dynamic Languages; C#,Visual Basic are Static Languages.

What makes a language “Dynamic”?
Dynamic Languages that execute at runtime many common behaviors that other languages might perform during complication, if at all. These behaviors could include extension of the program, by adding new code, by extending objects and definitions, or by modifying the type system, all during program execution. These behaviors can be emulated in nearly any language of sufficient complexity, but dynamic languages provide direct tools to make use of them.

Especially execution of clients at runtime is pretty good for web sites.

A Variable is a named location that stores a value. We can use the variables via the name we give them. These are the rules for naming a variable.

IronPython is dynamically typed, no pre-declaration of a variable or its type is necessary. Variables are created when they are first assigned values. Dynamic Languages are strongly typed languages, not weakly typed.

So if you create a integer variable, you won’t verifed up it ;)

Will Dynamic Languages provide us any conveniences?
The biggest problem for programmers is variable data type declaration. We can use this feature flexibly. Anyway we will initialize when we assign the declerated data type. And also one variable for only that data type.  So why do we declarate that data types?
 
Like this comic character, pre-declaration of data types is like writing names on the existing wellknown objects.

So IronPython does it for us :)

You know “int” stores integer from -2.147.483.647 to +2.147.483.647. If we assign a value over this limits we gave an overflow exception.

Now Let’s see this in a sample.

Let’s multiply x with 2, x is 2,000,000,000;

x * 2
x = 2,000,000,000

in Python/IronPython
x * 2 = 4,000,000,000

in Ruby/IronRuby
x * 2 = 4,000,000,000

My 8 year-old nephew calculate
find x * 2 = 4,000,000,000

but in C# as an int
x * 2 = -294,967,296 // overflow exception...

No pre-declaration of data types gives us an advantage. In this example we can easily see an advantage of dynamic languages.

>>>def fact(sayi):
... if sayi == 1:
...... return 1
... return sayi * fact(sayi-1)

>>> fact(150)
571338395644585459047893286526105400318955357860112641825483758
331798291248453983931265744886753111453771078787468542041626662
501986845044663559491959220665749425920957357789293253572904449
624724054167907221184454371222696755200000000000000000000000000
00000000000l

>>> fact(200)
788657867364790503552363213932185062295135977687173263294742533
244359449963403342920304284011984623904177212138919638830257642
790242637105061926624952829931113462857270763317237396988943922
445621451664240254033291864131227428294853277524242407573903240
321257405579568660226031904170324062351700858796178922222789623
703897374720000000000000000000000000000000000000000000000000l

Even we can calculate 1000 factorial without overflow exception.

def
fact(x): return (1 if x==0 else x * fact(x-1))

And now, what can we do with this flexiblity of IronPython?

>>> x = "ibrahimkivanc"
>>> print x
ibrahimkivanc

>>> x = 10
>>> print x
10

>>> x = 10.53
>>> print x
10.53

>>> x = True
>>> print x
True

>>> x=["ipy_array","first","second"]
>>> print x
['ipy_array', 'first', 'second']



Here is the flexibilty! first we assigned x variable to an integer. As you see we assign 5 different types of data value; string, integer, float, bool and array. We can give many sample like this.

With there is no type validation, especially at XNA game development, it will really help us. Maybe in the future there will be a IronPython entegration for XNA Game development.

PDF version of this article >> 2 -IronPython Dynamic Languages

If you have any questions or discover any errors / typos please let me know ik@ibrahimkivanc.com

All The Best!

Yorum ekle


 

  Country flag

biuquote
  • Yorum
  • Canlı önizleme
Loading