Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you implement an interface in IronPython?

The FAQ that comes with IronPython 2.0.1 says the following:

You can define interfaces in C#, build those into a DLL, and then implement those interfaces in Python code as well as pass the python objects that implement the interfaces to C# code.

I have googled and googled and googled, but haven't found how to do this. Can someone help?

like image 282
Rohit Avatar asked Mar 30 '09 04:03

Rohit


People also ask

How are interfaces implemented in C#?

On implementation of an interface, you must override all of its methods. Interfaces can contain properties and methods, but not fields/variables. Interface members are by default abstract and public. An interface cannot contain a constructor (as it cannot be used to create objects)

Can we implement interface in Python?

Unfortunately, Python doesn't have interfaces, or at least, not quite built into the language. Enter Python's abstract base class, or, cutely, ABC. Functionally, abstract base classes let you define a class with abstract methods, which all subclasses must implement in order to be initialized.

WHAT IS interface and implementation in Python?

In object-oriented languages like Python, the interface is a collection of method signatures that should be provided by the implementing class. Implementing an interface is a way of writing an organized code and achieve abstraction.

WHAT IS interface in C# MSDN?

An interface defines a contract. Any class or struct that implements that contract must provide an implementation of the members defined in the interface. An interface may define a default implementation for members. It may also define static members in order to provide a single implementation for common functionality.


1 Answers

You can do it with the regular inheritance syntax of Python:

class SomeClass (ISomeInterface):
    def SomeMethod(self, parameter):
        pass

Just "inherit" the interface, implement its methods as you would any other class method, and enjoy!

like image 139
Neil Williams Avatar answered Sep 28 '22 16:09

Neil Williams