[IronPython] IronPyton newbie questions

Martin Maly Martin.Maly at microsoft.com
Tue Sep 12 17:55:02 CEST 2006

Great documentation on Python in general is available at http://www.python.org/doc/
As for IronPython specific information, you can refer to the tutorial included with the IronPython distribution, check out our wiki at http://www.codeplex.com/ironpython, specifically the page http://www.codeplex.com/Wiki/View.aspx?ProjectName=IronPython&title=More%20Information

As for your specific questions:

- conditional compilation is not part of Python language. However, Python does provide tools which can help you achieve something quite similar. You can conditionally define functions, for example:

if condition:
    def func(): return 1
    def func(): return 2

print func()    # the depending on the condition, the function "func" will have different value and therefore do different things

- .NET attributes are not yet supported by IronPython

- properties - Python does support construct similar to .NET properties:

class C(object):
    def __init__(self): self.__value = 0

    def getter(self): return self.__value
    def setter(self, value): self.__value = value

    m = property(getter, setter)

c = C()
print c.m
c.m = 10
print c.m

If you meant using properties on .NET objects, that works also:

>>> import System
>>> System.Environment.CurrentDirectory # static property

- the "is" is an operator which checks for object identity. The reason you can do "condition is True" is that value of a condition (if true or false) always returns the same _instance_ of True. Checking for object identity is faster than testing for equality, but not always the correct thing to do, for example:

>>> x = 123456 + 123456
>>> x
>>> y = 246912
>>> x == y                      # Same value
>>> x is y                      # But different instance

- delegates: IronPython will automatically convert any Python method into a .NET delegate and ensures the type of the delegate matches. For example:

IronPython 1.0.2445 on .NET 2.0.50727.42
Copyright (c) Microsoft Corporation. All rights reserved.
>>> import clr
>>> clr.AddReference("System.Windows.Forms")
>>> def Click(*args): print args
>>> import System.Windows.Forms as Forms
>>> f = Forms.Form()
>>> f.Click += Click            # This took the Python method "Click", turned it into
                                        # a delegate and added it as the event listener

- structs: You can certainly use .NET structs inside IronPython with some limitations, see http://www.codeplex.com/Wiki/View.aspx?ProjectName=IronPython&title=Value%20Types for more details. As for constructing structs, it is not possible to define a .NET struct in IronPython. The closest you'll probably get is using new-style classes (classes that have "object" in their inheritance hierarchy) with __slots__ (this actually allocates slots for the members as opposed to putting them into a dictionary. Once you define __slots__, attributes not specified within __slots__ are not accessible (unless one of your __slots__ is __dict__ :)

>>> class C(object):
...     __slots__ = ['a', 'b']
>>> c = C()
>>> c.a = 10
>>> c.b = 10
>>> c.c = 10
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
AttributeError: 'C' object has no attribute 'c'

Hope this helps

-----Original Message-----
From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of David Anton
Sent: Saturday, September 09, 2006 4:51 PM
To: users at lists.ironpython.com
Subject: [IronPython] IronPyton newbie questions

I'm new to IronPyton (and Python in general).
Where can I find a good reference or spec for IronPython?

I'd like to find information on whether the following is available in
IronPython (google has been of limited help so far in the following
- conditional compilation
- attributes (e.g., System.SerializableAttribute)
- properties
- use of "is" (for example, why is "is" used with the boolean literal
- defining delegates
- structs (as in C#)

View this message in context: http://www.nabble.com/IronPyton-newbie-questions-tf2246006.html#a6229361
Sent from the IronPython forum at Nabble.com.

users mailing list
users at lists.ironpython.com

More information about the Ironpython-users mailing list