1

So I'm trying to make a subclass of DataGridViewColumn, which has both a no-argument constructor, and a constructor that takes one argument, which expects type DataGridViewCell. This is my class:

class TableColumn(DataGridViewColumn):
    def __init__(self, string):
        super(TableColumn, self)
        self.Text = string
        self.CellTemplate = DataGridViewTextBoxCell()
        self.ReadOnly = True

Whenever I try to pass in a string as the argument like such:

foo = TableColumn('Name')

It always gives me this:

TypeError: expected DataGridViewCell, got str

So it seems that it's always passing 'string' to the single-argument constructor of the superclass. I've tried replacing super(TableColumn, self) with super(TableColumn,self).__init__() to be explicitly sure that I want to call the no-argument constructor, but nothing seems to work.

6
  • I'm not super familiar with IronPython, but the __init__() seems like it should be fine. Maybe the whole traceback would be helpful. Commented Jun 14, 2012 at 22:25
  • The only thing is the traceback doesn't show me what happens past calling the constructor, so there's no useful information. Commented Jun 14, 2012 at 23:04
  • Hmm so it seems that it's an error with Python 2.7 (which the latest version of IronPython uses), which doesn't allow this. Commented Jun 14, 2012 at 23:28
  • Python 2.7 definitely allows this. Unless there's something weird about the parent class. But did you mean to call super(TableColumn, self) rather than, say, super(TableColumn, self).__init__()? Commented Jun 15, 2012 at 0:08
  • Also, is that TypeError being raised by TableColumn's init method, or from somewhere else? Commented Jun 15, 2012 at 0:09

1 Answer 1

1

You actually don't want to implement __init__ when deriving from a .NET class; you need to implement __new__ instead.

class TableColumn(DataGridViewColumn):
    def __new__(cls, string):
        DataGridViewColumn.__new__(cls)
        self.Text = string
        self.CellTemplate = DataGridViewTextBoxCell()
        self.ReadOnly = True

Basically, the constructors of .NET base classes need to be called before the Python subclass' __init__ (but after __new__), which is why you were getting the wrong DataGridViewColumn constructor called.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.