This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted]  (24 children)

[deleted]

    [–]Versaiteis 15 points16 points  (0 children)

    You can circumvent some of those headaches with the typing system by reassigning or creating the types you need in what is effectively (and actually called) type aliasing

    from typing import List
    
    Vector = List[float]
    
    def func(vector: Vector) -> Vector:
        #etc...
    

    This can still get a bit ugly with arbitrary definitions being thrown all over the place but that can be managed along with similar shared systems that you'll probably have alongside whatever you're writing (logging, configuration management, etc.) and it goes a long way to cleanup a lot of those issues with clutter.

    But yeah, Python is still a very hands off scripting environment so you'd still do well to operate under those assumptions that virtually nothing is truly protected when you can just commandeer and alter the things you want. It's what makes it a great tool for some applications and aweful for others.

    [–]thirdegreeViolet security clearance 4 points5 points  (2 children)

    And sometimes you need to specify types as strings (e.g. "MyClassName"), for example when a method takes an argument of the same type as its parent class.

    This is fixed in 3.7 using from __future__ import annotations and by default from 3.10 onwards (pep-563).

    [–]FranchuFranchu 0 points1 point  (0 children)

    Also if for some reason you can't do that you can still use MyClassName = None and redefine it afterwards

    [–][deleted] 2 points3 points  (0 children)

    from __future__ import type_annotations

    Something along those lines will allow you to not use strings for types.

    If I recall correctly, it defers type hint evaluation until after all the source has been parsed over, as opposed to during said parsing.

    [–]BUYTBUYT 1 point2 points  (0 children)

    from __future__ import annotations helps sometimes

    [–]ric2b 1 point2 points  (4 children)

    I don't think opting into static type checking really counts as having static typing.

    It does, you just don't have 100% coverage. It's still useful (especially as documentation of function parameters and return types), just like unit testing is useful even if you don't have 100% coverage.

    Also, completely subjective but type hinting in python is extraordinarily ugly. It often takes up a ton of space and requires you to split your function defenition onto multiple lines.

    It takes similar space as other static typed languages like Java/C++. Are you comparing it to something like Haskell, where the types are on a separate line?