0

I have the following C# public classes:

public class ClassA
{
    public byte var1;
    public TypeX var2;
}


public class TypeX
{
    public UInt16 num1;
    public UInt32 num2;
}

(Names are simplified for this example and are not real)

What would this look like in Python? Specifically, how would var2 be created as an instance of TypeX?

So far I have the come up with the following Python classes:

class ClassA():
    def __init__(self, var1, var2 = TypeX):
        self.var1 = var1
        self.var2 = var2


class TypeX():
    def __init__(self, num1, num2):
        self.num1 = num1
        self.num2 = num2

However, I am nearly 100% sure this is not correct. Should ClassA inherit TypeX? Any guidance or explanations would be appreciated!

1
  • 1
    They aren't equivalent because you aren't assigning anything to your c# variables. You'd get a NullReferenceException when you attempted to access var1 or var2 from ClassA. This can be easily remedied with a constructor. Commented Sep 18, 2018 at 16:07

3 Answers 3

2

Python is a dynamically-typed language. The type of a variable is not determined until it is assigned a value.

The closest you can get to your C# code would be to use type hints, supported in Python 3.5+:

class TypeX:
    def __init__(self, num1, num2):
        self.num1 = num1
        self.num2 = num2

class ClassA:
    def __init__(self, var1, var2: TypeX):
        self.var1 = var1
        self.var2 = var2

But even with type hints, the type of var2 will not be enforced either at compilation time or at run time.

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

Comments

1

Python is not a statically typed language, so the answer to:

How can I enforce var2 to be of type TypeX?

is you don't. Of course, you can type-check something, but the Python interpreter won't do it for you, you'll need to explicitly write this rule on your code programatically.

As to the question of how the following:

public class ClassA
{
    public byte var1;
    public TypeX var2;
}


public class TypeX
{
    public UInt16 num1;
    public UInt32 num2;
}

translates to Python, the answer is as simple as:

# You need to explicitly initialize the variables to their correspondent default values
class ClassA(object):
    def __init__(self):
        self.var1 = bytes() # or b''
        self.var2 = None

class TypeX(object):
    def __init__(self):
        self.num1 = int() # or 0
        self.num2 = int() # or 0

5 Comments

Right, I knew Python didn't enforce types. Thanks for the clarification on variables assignments in __init__.
I assume bytes() and int() typecasts whatever value is sent to self.... Is it not possible to do = TypeX() for var2? I'm guessing not, but why?
@CtrlS it’s possible indeed. But then it wouldn’t exactly match your C# code.
By the way, bytes() and int() does not type cast anything. They are exactly the same as writing b'' and 0
Interesting... so that's why the comments are as such.
1

Python is dynamically, not statically, typed, so

class ClassA():
    def __init__(self, var1, var2):
        self.var1 = var1
        self.var2 = var2


class TypeX():
    def __init__(self, num1, num2):
        self.num1 = num1
        self.num2 = num2

is a reasonable thing to do. Just pass an instance of TypeX in when you instantiate your ClassA.

However, as of Python 3.5, you can add optional type hints, e.g.

class TypeX():
    def __init__(self, num1, num2):
        self.num1 = num1
        self.num2 = num2


class ClassA():
    def __init__(self, var1, var2: TypeX):
        self.var1 = var1
        self.var2 = var2

This doesn't affect how the code runs (in particular it doesn't enforce the type at runtime), but it can help developers, and their tools, understand the code better.

Tools like mypy can be used to statically check your code using these optional type hints, especially if you add them to your test suite. The more hints you add, the more mypy can help.

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.