Typing, how come that :int is not ensuring int parameter?

zljubisic at gmail.com zljubisic at gmail.com
Thu Jun 11 16:51:25 EDT 2020


Hi,

If I run this code:
class Property:

    def __init__(self, var: int):
        self.a: int = var

    @property
    def a(self):
        return self.__a

    @a.setter
    def a(self, var: int):
        if var > 0 and var % 2 == 0:
            self.__a = var
        else:
            self.__a = 2

if __name__ == '__main__':
    x = Property(1.5)
    print(x.a)

I am getting 2.
How come, if I have created a class with :int in order to ensure that "a" property must be an integer, that I can create an instance with float (1.5)?

Regards.


More information about the Python-list mailing list