1

I'm relatively new to Python and despite googling, I can't see how this works. Why are the last two lines printing the wrong data type. I understand the difference between string and int, but want to understand other numerics like float, doubles etc and how I can ensure the right data type..

In c#, the constructors would take care of the class. How do we achieve the same in python?

   class Results:
        Product: [str]
        Total_Value: [int]
        Quantity: [int]
        ProductID: [str]
    
        def __init__ (self, Product: str, Total_Value: int, Quantity: int, ProductID: str):
                self.Product=Product
                self.Total_Value=Total_Value
                self.Quantity=Quantity
                self.ProductID=ProductID
    
     
    
    
    r=Results("hello",1.88,3.888,888)
    print(r.ProductID)
    print(type(r.ProductID))  # why is thie not a string? ? This prints 'int' but ProductID should be string
    print(type(r.Total_Value))  # why is this not an int? This should be 'int' but Total_Value is 'float'


 
    

In c#, the output is correct and I was expecting something similar in Python

void Main()
{
    var b =new Bunny();
    b.Name="Tom";
    b.number=3;
    b.number2=3;
    
    Console.WriteLine(b.number.GetType()); //this prints Int64 as it should
    Console.WriteLine(b.number2.GetType()); //this prints Double as it should
    
    
}

public class Bunny
{
    public string Name;
    public System.Int64 number;
    public double number2;
 
}

   
3
  • 1
    First of all, [int] is not a valid PEP 484 type. Second, type annotations in Python are never enforced by the Python interpreter, only by external tools like mypy. Third, you passed an integer for ProductID. Even if Python did enforce type signatures on its own (which it doesn't), that would be an error. If you pass an integer where a string is expected in C#, that's an error, not some sort of exciting but questionably-desgined implicit coercion Commented Oct 23, 2021 at 5:52
  • # why is thie not a string? because you very clearly passed an int object as the 4th argument to the constructor: r=Results("hello",1.88,3.888,888), which is the parameter that gets assigned to self.ProductID. Why did you expect anything different? Commented Oct 23, 2021 at 6:03
  • Thanks for clearing up my understanding. Then what is the purpose of stating the type then? That is, if type annotations are enforce, then what is the difference between the first and second below or what purpose is there in stating the type: def __init__(self, Product: str, Total_Value: int, Quantity: int, ProductID: str): ...blah blah def __init__(self, Product, Total_Value, Quantity, ProductID): ... blah blah Commented Oct 23, 2021 at 9:32

1 Answer 1

1

If I'm not wrong, at ProductID enter value must be "888" not just 888. In Total_Value float is a decimal number like 1.88(like from your code).

e.g. 1 is integer, "1" is string, 1.00 is float, if you want to change datatype you can use function like str(),int().

Hope this will help you, I'm also new on stackoverflow so if anything wrong I really sorry.

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

1 Comment

Thanks for the feedback, the string was just a a demo. What I wanted was to enforce a numeric type such as double, decimal, float etc. Another words, what is the purpose of the datatype below if they're not enforced? class GoodPerson: def init (self, Firstname: str, Lastname: str, Age: int): self._FirstName=Firstname self._Lastname=Lastname self._Age=Age

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.