It’s still very immature, but I would like to improve it in the future by adding function overloading and other features. It implements the typechecked decorator described before, and some other useful things like structural interfaces, unions and logic predicates that can be used as function annotations. I started a small library to work with type annotations. Personally, I would love to see this feature more used in the Python community. A recent discussion in the Python-ideas mailing list unveiled this problem. The downside of this flexibility is that it causes some confusion in the community about how annotations should be used. PEP 3107 introduced function annotations in Python. Then I realized that Python had syntax for annotations years before TypeScript or Dart were even planned. While playing with TypeScript I couldn’t stop thinking how cool would be to have something like that in Python. You can discover the API while you type and use automatic re-factor tools without worrying about breaking code.Īnders Hejlsberg’s introduction video to TypeScript contains more interesting details about how annotations are really useful. With good code completion you don’t have to browse huge API docs looking for what you need. Another interesting thing is accurate code completion. By knowing that title is a string, it knows that findTitle is returning a string too, and therefore t is a string and strings don’t have an innerHTML method.Įarly error detection is one advantage of good tooling support. Now let’s see the TypeScript Web Editor with the same code adding a single type annotation to findTitle: The code has a small error that is not very easy to notice. Var titleElement = document.getElementById('title-' + title) Let me show you a simple example of how this can be seriously useful, consider the following Javascript code: These have no different purpose than aiding editors and IDEs. They’re interesting because, despite being dynamically typed languages, both implement optional type annotations. These are languages that compile to Javascript and both try to improve tooling support. I’ve been playing with Dart and TypeScript recently. It’s impossible for the editor to give you any hint about a or b. The problem with dynamically typed languages is that it’s impossible for the IDE to infer things about some parts of your code. I disagree, IDEs are a truly valuable tool and the “nice language or IDE” statement is a false dilemma. Some people say that IDEs are a language smell. C# and Java have powerful IDEs that can improve your productivity significantly. Especially as a beginner, you should think very explicitly about types, and type annotations can help you with this.Something I’ve always missed when using Python (and dynamically typed languages in general) is nice tooling support. Duck typing and using try-except blocks instead of using type checking is an advanced concept that is far more difficult for beginners to understand or implement. People often mistake Python's unintimidating syntax with the language being easy. In the example above, it really was useful to know that the program function requires a BeautifulSoup object, and I feel that it's much more appropriate than duck-typing here. I think that being too rigorous with this rule can decrease code quality. Some Pythonistas are opposed to type annotations, as they consider it unpythonic. Whether you use type annotations, and how often, will depend on the kind of applications you write and your own style. This can prevent a lot of runtime errors from occurring, and can thus make your code more reliable. Third, while the standard Python interpreter doesn't check whether the argument types match those specified in the function definition, external tools and IDEs can do this. Without type annotations, it would take a long time to figure out what program was supposed to be if it had been a long time since I'd worked on the code, or if the application was sufficiently large. It's a BeautifulSoup object that implements a find method. Now it's entirely clear what program is supposed to be. If you're familiar with Java, this would be somewhat equivalent (see below): void sayHello (String name)).text.strip() In the function definition, we specify that name should be a str and that we expect that nothing will be returned. In case you're unfamiliar with type annotations, it looks like this: def say_hello (name: str) -> None: Python 3.5 introduced type annotations, or type hints, as proposed in PEP-483 and PEP-484.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |