# Creating Python Classes Derived from C# Class

**URL:** <https://forum.opentap.io/t/creating-python-classes-derived-from-c-class/353>\
**Category:** Technical\
**Tags:** python\
**Created:** [June 22, 2021, 10:13pm UTC](https://forum.opentap.io/t/creating-python-classes-derived-from-c-class/353 "2021-06-22T22:13:59Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![john.berlien](https://avatars.discourse-cdn.com/v4/letter/j/ac91a4/32.png) [@john.berlien](https://forum.opentap.io/u/john.berlien)\
**Post date:** [June 22, 2021, 10:13pm UTC](https://forum.opentap.io/t/creating-python-classes-derived-from-c-class/353/1 "2021-06-22T22:13:59Z")

</div>

Sorry if this is a repeat, but I don’t think it is.

I’m wondering if there’s a way to create an OpenTAP Python class that gets included in a plugin (deriving from Instrument, TestStep, DUT, etc.) and is made available in TAP, that derives directly from a C# class. For example, if I want to create a Python Instrument class that derives from the C# ScpiInstrument class, it doesn’t get included (compiled? transpiled?) in the plugin. If I’m unable to do this, this means that all the SCPI features of OpenTAP are unavailable in Python, if I’m not mistaken.

---

<div class="post-metadata">

**Author:** ![brennen\_direnzo](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opentap.io/brennen_direnzo/32/6_2.png) [@brennen\_direnzo](https://forum.opentap.io/u/brennen_direnzo)\
**Post date:** [June 22, 2021, 10:41pm UTC](https://forum.opentap.io/t/creating-python-classes-derived-from-c-class/353/2 "2021-06-22T22:41:01Z")

</div>

@john.berlien I’m not sure I am completely following. In general you can mix C# and Python, either calling C# from Python or Python from C#. That is the main benefit provided from [Python.NET](http://Python.NET), and part of the Python plugin provides this wrapper. But for ScpiInstrument specifically, you can see this somewhat done in this example:  
[TapApiFromPython/TapPythonInstrumentIo.py · master · OpenTAP / Plugins / python · GitLab](https://gitlab.com/OpenTAP/Plugins/python/-/blob/master/TapApiFromPython/TapPythonInstrumentIo.py)

---

<div class="post-metadata">

**Author:** ![john.berlien](https://avatars.discourse-cdn.com/v4/letter/j/ac91a4/32.png) [@john.berlien](https://forum.opentap.io/u/john.berlien)\
**Post date:** [June 22, 2021, 10:52pm UTC](https://forum.opentap.io/t/creating-python-classes-derived-from-c-class/353/3 "2021-06-22T22:52:50Z")

</div>

Thanks for the link Brennen! That is interesting - but not what I was referring to. I meant something like this:

```csharp
from PythonTap import *

import OpenTap

from OpenTap import ScpiInstrument

@Attribute(OpenTap.DisplayAttribute, "SCPI VNA", "VNA driver based on ScpiInstrument class.", "My Python Plugin", "VNA")

class ScpiVNA(ScpiInstrument):

    """SCPI-based VNA class"""

    def __init__ (self):

        super(ScpiVNA, self). __init__ ()

        self.Name = "ScpiVNA"

        self.VisaAddress = "TCPIP0::localhost::hislip0::INSTR"

```

It’s not getting included in the plugin build process. From reading through PythonTap.py, doesn’t seem like it’s meant to. It doesn’t look like Python subclasses of _C# classes_ are meant to be included as part of the “plugin types”.

---

<div class="post-metadata">

**Author:** ![brennen\_direnzo](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opentap.io/brennen_direnzo/32/6_2.png) [@brennen\_direnzo](https://forum.opentap.io/u/brennen_direnzo)\
**Post date:** [June 22, 2021, 11:03pm UTC](https://forum.opentap.io/t/creating-python-classes-derived-from-c-class/353/4 "2021-06-22T23:03:17Z")

</div>

Yeah this might be a gap from the instrument side specifically. What we have seen more so far is that an Instrument Plugin is entirely C#, like it could be for a VNA for instance, and then the Test Steps are Python. But to be honest I’m not sure if this was intentional/limitation, or if it is just a gap since someone hasn’t needed it yet. @rolf_madsen ideas?

---

<div class="post-metadata">

**Author:** ![john.berlien](https://avatars.discourse-cdn.com/v4/letter/j/ac91a4/32.png) [@john.berlien](https://forum.opentap.io/u/john.berlien)\
**Post date:** [June 22, 2021, 11:37pm UTC](https://forum.opentap.io/t/creating-python-classes-derived-from-c-class/353/5 "2021-06-22T23:37:45Z")

</div>

Yes I figured that’s how you’d get around it, since you _can_ use C# classes in Python. To me, this is a more significant limitation, as this would apply to _any_ OpenTAP classes written in C#; you wouldn’t be able to build on them in a Python plugin. I think ScpiInstrument is just the most obvious example. But this means an existing OpenTAP code base couldn’t be built on in Python.

---

<div class="post-metadata">

**Author:** ![rolf\_madsen](https://avatars.discourse-cdn.com/v4/letter/r/43a26b/32.png) [@rolf\_madsen](https://forum.opentap.io/u/rolf_madsen)\
**Post date:** [July 1, 2021, 8:01am UTC](https://forum.opentap.io/t/creating-python-classes-derived-from-c-class/353/6 "2021-07-01T08:01:19Z")

</div>

Creating a new base class that is supported in all aspects in OpenTap is quite complicated.

It is however possible to build on plugins created in OpenTap, you’ll just have to not use inheritance.

---

<div class="post-metadata">

**Author:** ![john.berlien](https://avatars.discourse-cdn.com/v4/letter/j/ac91a4/32.png) [@john.berlien](https://forum.opentap.io/u/john.berlien)\
**Post date:** [July 1, 2021, 1:58pm UTC](https://forum.opentap.io/t/creating-python-classes-derived-from-c-class/353/7 "2021-07-01T13:58:34Z")

</div>

Oh - so you mean essentially create a wrapper around the class? Simply expose the methods and properties written in C# through Python with a new Python class, inheriting one of the existing supported base classes? Not elegant, like using inheritance itself… But doable I suppose.

What would you think about using ScpiInstrument as an example of doing this? Put that into the Python plugin somewhere.

---

<div class="post-metadata">

**Author:** ![rolf\_madsen](https://avatars.discourse-cdn.com/v4/letter/r/43a26b/32.png) [@rolf\_madsen](https://forum.opentap.io/u/rolf_madsen)\
**Post date:** [July 2, 2021, 7:31am UTC](https://forum.opentap.io/t/creating-python-classes-derived-from-c-class/353/8 "2021-07-02T07:31:52Z")

</div>

That is not exactly what I mean, but yes, if what you want is a PythonScpiInstrument that is maybe exactly what I am saying… I didn’t think it through that way. Anyway, how many methods do you really need on there? 🙂

---

<div class="post-metadata">

**Author:** ![john.berlien](https://avatars.discourse-cdn.com/v4/letter/j/ac91a4/32.png) [@john.berlien](https://forum.opentap.io/u/john.berlien)\
**Post date:** [July 2, 2021, 4:14pm UTC](https://forum.opentap.io/t/creating-python-classes-derived-from-c-class/353/9 "2021-07-02T16:14:21Z")

</div>

Just the public ones of course would need a wrapper method in Python.
