[IronPython] Constructors & inheriting from standard .NET classes

Zach Crowell zachc at microsoft.com
Sat Aug 22 02:02:20 CEST 2009


Thanks.

-----Original Message-----
From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Dino Viehland
Sent: Friday, August 21, 2009 16:52
To: Discussion of IronPython
Subject: Re: [IronPython] Constructors & inheriting from standard .NET classes

Yes, you're right, too much copying and pasting :)

> -----Original Message-----
> From: users-bounces at lists.ironpython.com [mailto:users-
> bounces at lists.ironpython.com] On Behalf Of Michael Foord
> Sent: Friday, August 21, 2009 4:45 PM
> To: Discussion of IronPython
> Subject: Re: [IronPython] Constructors & inheriting from standard .NET
> classes
>
> Dino Viehland wrote:
> > You need to override and call the base __new__ instead of __init__.
> .NET has a simpler construction model than Python does and __new__ is
> what best corresponds to .NET constructors.
> >
> > class Derived(Test.Base):
> >     def __new__(cls, i):
> >         return Test.Base.__new__(cls, i)
> >
> > d = Derived()
> >
>
> Won't that still blow up? What will .NET use for i in the constructor
> if
> you don't provide an argument?
>
> Michael
>
> >
> > From: users-bounces at lists.ironpython.com [mailto:users-
> bounces at lists.ironpython.com] On Behalf Of Zach Crowell
> > Sent: Friday, August 21, 2009 4:39 PM
> > To: users at lists.ironpython.com
> > Subject: [IronPython] Constructors & inheriting from standard .NET
> classes
> >
> > I am unable to inherit from .NET classes which do not define a
> parameterless constructor.  Is this expected behavior? Is there another
> way to make this inheritance work?
> >
> > Here's a simple case.
> >
> > using System;
> >
> > namespace Test
> > {
> >     public class Base
> >     {
> >         public Base(int i)
> >         {
> >         }
> >     }
> > }
> >
> > import clr
> > clr.AddReference('Test')
> > import Test
> >
> > class Derived(Test.Base):
> >     def __init__(self):
> >         pass
> >
> > d = Derived()
> >
> > Traceback (most recent call last):
> >   File "d:\tmp\class.py", line 9, in d:\tmp\class.py
> > TypeError: Derived() takes exactly 2 arguments (1 given)
> > _______________________________________________
> > Users mailing list
> > Users at lists.ironpython.com
> > http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
> >
>
>
> --
> http://www.ironpythoninaction.com/
> http://www.voidspace.org.uk/blog
>
>
> _______________________________________________
> Users mailing list
> Users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com

_______________________________________________
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