[IronPython] Problem with 3 modules, classes and missing attributes

Michael Foord fuzzyman at voidspace.org.uk
Sun Apr 6 02:13:50 CEST 2008


Hello Pjerrot,

Unfortunately the layout of code created by Visual Studio IronPython (do 
you mean IronPython Studio or Visual Studio Professional?) is a 
*terrible* way to write Python.

It effectively matches the semantics of C#, but you should not replicate 
it yourself - do not contain your classes inside a namespace class. 
Additionally, do not use the 'from XXX import *' - but instead 
explicitly import the names you want to use.

I don't say the last point out of dogma - I think if you make that 
change you will immediately see what is wrong with the code.

All the best,


Michael Foord
http://www.ironpythoninaction.com/


Pjerrot wrote:
> Hi,
>
> I have problem understanding IronPython. The problem is here:
>
> Traceback (most recent call last):
>   File , line 0, in <stdin>##9
>   File , line 0, in __import__##4
>   File ..\NamespaceTest\program.py, line 28, in Initialize
>   File ..\NamespaceTest\program.py, line 24, in start
>   File ..\NamespaceTest\program.py, line 9, in testBar
> AttributeError: type object 'ns2' has no attribute 'Bar'
>
> I try to follow the namspace style created by Visual Studion
> IronPython. A namspace is cerated as a class, without a constructor.
> Inside the class is another class. The outer class works like a
> namespace.
>
> I create two files each holding a class, Bar and Foo. The files start
> with the same "class ns2:" for namspace "ns2". So Bar and Foo belong
> to the ns2 namespace. But when I try from another namspace "ns1" to
> uit test the classes. The ns2 namespace miss oe of the classes.
>
> Please give me an explanation. And, if you can, how can I structure
> the python code like C#.
>
> Hope somebody can help
>
> Pjerrot
>
>
> Attached are the three files: program.py, Bar.py and Foo.py.
> ----------------------------------
> program.py:
> from Bar import *
> from Foo import *
>
> class ns1:  #namespace
>     def testBar():
>         bar = ns2.Bar(1)
>         if bar.Y == 1:
>             print "Bar.Y test OK"
>         else:
>             print "Bar.Y test failed"
>     def testFooBar():
>         b = ns2.Bar.Bar(1)
>         fb = ns2.FooBar(b)
>         if fb.B == b.B:
>             print "FooBar.B test OK"
>         else:
>             print "FooBar.B test failed"
>
>     @staticmethod
>     def start():
>         ns1.testBar()
>         ns1.testFooBar()
>
> if __name__ == "program":
>     ns1.start()
> -------------------------------------
> Bar.py:
> class ns2:  # namespace
>
>     class Bar(object):
>         def __init__(self, y=0):
>             self._y = y
>
>         # Y property
>         def getY(self):
>             return self._y
>         def setY(self, value):
>             self._y = value
>         Y = property(getY,setY)
> ------------------------------------------
> Foo.py
> class ns2:  # namespace
>
>     class FooBar(object):
>         def __init__(self,b=None):
>             self._b = b
>
>         # B property
>         def getB(self):
>             return self._b
>         def setB(self, value):
>             self._b = value
>         B = property(getB,setB)
> -------------------
> _______________________________________________
> Users mailing list
> Users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>   




More information about the Ironpython-users mailing list