python 打字游戏_python开发人员的打字稿

python 打字游戏

If you already know Python, you might be interested in learning TypeScript. It is getting more and more popular. It works in your browser. And in many facets, it is a very similar language.

如果您已经了解Python,则可能对学习TypeScript感兴趣。 它越来越受欢迎。 它可以在您的浏览器中使用。 在许多方面,它是一种非常相似的语言。

However, most places to learn a programming language are focused on new developers. Very few articles or tutorials cut down to the essentials of the new language while leaving out the basic of programming. This article will provide a brief but dense overview how TypeScript works, and how you can get started easily. It contains the basic syntax, loops, types, shorthands are more.

但是,大多数学习编程语言的地方都针对新开发人员。 很少有文章或教程介绍新语言的要点,而忽略了编程的基础。 本文将简要扼要介绍TypeScript的工作原理以及如何轻松入门。 它包含基本语法,循环,类型,简写等。

基础 (The basics)

TypeScript is a superset of Javascript. So any Javascript code is valid TypeScript code but not the other way around. TypeScript compiles into JavaScript code, which can then be interpreted by the executor, a browser or NodeJS.

TypeScript是Javascript的超集。 因此,任何Javascript代码都是有效的TypeScript代码,但反之则不是。 TypeScript编译为JavaScript代码,然后可以由执行程序,浏览器或NodeJS解释。

TypeScript can be used as a front end language, generally as part of a framework or library. Or as a backend with node.js, often as a webserver but in theory also as a pure backend.

TypeScript可以用作前端语言,通常用作框架或库的一部分。 或作为node.js的后端,通常作为Web服务器,但从理论上讲也作为纯后端。

句法 (Syntax)

Like most other languages, TypeScript uses brackets to create scopes, versus Python’s indentation based style. CamelCase TypeScript is also the convention versus Python’s snake_case .

与大多数其他语言一样,TypeScript使用括号创建作用域,而不是Python的基于缩进的样式。 CamelCase TypeScript还是Python的snake_casesnake_case

基本 (Basics)

Here are a list of basics to get started, first as Python code and then TypeScript code. I’ll use Python’s typing to compare to TypeScripts typing. TypeScript’s typing is actually also optional, and can infer types very well.

这里是入门的基础列表,首先是Python代码,然后是TypeScript代码。 我将使用Python的输入与TypeScripts的输入进行比较。 TypeScript的键入实际上也是可选的,并且可以很好地推断类型。

Basic syntax
基本语法

平等 (Equality)

There are two equality operators in JavaScript and TypeScript. == checks for value equality. This will attempt to typecast the values to eachother. === will check for value equality and for type.

JavaScript和TypeScript中有两个相等运算符。 ==检查值是否相等。 这将尝试将值相互转换。 ===将检查值相等性和类型。

This casting makes JavaScript do strange things some of the time. A lot of the WtfJs items are about equality due casting of the items.

这种转换使JavaScript有时会做一些奇怪的事情。 WtfJs的很多项目都与项目的强制转换有关。

作业 (Assignments)

First of all, assignment in TypeScript is done with a keyword. There are three key words: var, let and const. Learning this is quite easy: use const if the value is a constant and won’t change, use let is the value is going to change and don’t use var . The reason to not use var is about variable scoping, and is not covered here.

首先,TypeScript中的分配是通过关键字完成的。 有三个关键字: varletconst 。 学习起来很容易:如果值是一个常量并且不会改变,则使用const如果值将要改变,则使用let ,而不要使用var 。 不使用var的原因是关于变量作用域 ,这里不做介绍。

Because of this scoping and explicit assignment, you also need to define variables at the right time. For example, if you want to have a new variable be large in an if block, and small in the else block. Then you must make sure to define the variable before the if else block. If you don’t, the complier will complain because the variable does not exist outside of the if else block.

由于此作用域和显式分配,您还需要在正确的时间定义变量。 例如,如果要有一个新变量,则在if块中将其设为large ,在else块中将其设为small 。 然后,您必须确保在if else块之前定义变量。 如果您不这样做,则编译器将抱怨,因为该变量在if else块之外不存在。

循环和列表 (Loops and lists)

TypeScript has two type of for loops. One more classic one, one more modern one. Next to that, the Array prototype (aka the list type), has a couple of nice built in functions, like forEach, map, filterMost (but not forEach) return a new array and don’t modify the old array. This is very nice for the functional, pure functional folks who attempt to not change any variables and just make new ones.

TypeScript具有两种类型的for循环。 一种经典,一种现代。 紧接着,Array原型(又称列表类型)具有几个不错的内置函数,例如forEach mapfilter 大多数 (但不是forEach )返回一个新数组并且不修改旧数组。 这对那些不更改任何变量而只创建新变量的纯函数式函数的人来说非常好。

种类 (Types)

These compare mypy types with TypeScript. The format is Python — TypeScript. A complete list of TypeScript types can be found here.

这些将mypy类型与TypeScript进行比较。 格式为Python — TypeScript 。 可在此处找到TypeScript类型的完整列表。

  • int, float, double, long number

    int, float, double, long number

  • str string

    str string

  • List[T]Array<T> or T[], with T being any other type

    List[T] Array<T> T[] ,其中T是任何其他类型

  • Dict Object

    Dict Object

  • Tuple[str, int, dict][string, number, object]

    Tuple[str, int, dict] [string, number, object]

  • TypedDictCustom interface

    TypedDict 自定义界面

TypeScript and JavaScript only has one types for numbers without a specific reference to integers or floats. In some sense it makes sense, but makes casting to integers annoying.

TypeScript和JavaScript仅具有一种数字类型,而没有具体引用整数或浮点数。 从某种意义上讲,这是有意义的,但会使转换为整数变得烦人。

While talking about types, someone cannot skip the two different empty values in TypeScript: null and undefined.

在谈论类型时,有人不能跳过TypeScript中的两个不同的空值: null undefined

  • null denotes an non-existing object or the fact that there is no input. It can be compared to Python’s None.

    null表示不存在的对象或没有输入的事实。 可以将其与Python的None进行比较。

  • undefined is returned when an attribute is referenced that does not exist. It is similar to Python’s KeyError or AttributeError.

    当引用的属性不存在时,返回undefined 。 它类似于Python的KeyErrorAttributeError

TypeScript doesn’t error out if you reference an non-existing value on an object. However, referencing anything on undefined does throw a TypeError. Both are falsy, and undefined i snot equal to null.

如果在对象上引用不存在的值,TypeScript不会出错。 但是,在undefined上引用任何内容都会引发TypeError 。 两者都是虚假的, undefined等于null

A small difference in bool casting of types, is that an empty list and an empty object are truthy in TypeScript.

类型的布尔转换的一个小差异是,空列表和空对象在TypeScript中是正确的。

对象 (Objects)

Objects in TypeScript are very similar to dictionaries in Python.

TypeScript中的对象与Python中的字典非常相似。

There are some differences though:

但是有一些区别:

  • Each key also becomes an attribute. There is no real difference between someObject.key or someObject['key'], except that the TypeScript compiler might complain less with the second varation.

    每个键也成为一个属性。 someObject.keysomeObject['key']之间没有真正的区别,只是TypeScript编译器在第二种变体中的抱怨较少。

  • You cannot use variables in the creation, as keys are taken as strings in the assignment.

    您不能在创建中使用变量,因为键在分配中被当作字符串使用。
  • As metioned, referencing non-existing fields returns undefined.

    如前所述,引用不存在的字段将返回undefined

功能 (Functions)

Functions work much like in Python, with some small differences. Functions can have default values. However, function calls must be filled in left to right, and do not support keyword arguments. They do support a random number of arguments with spread parameters.

函数的工作方式与Python相似,但有一些细微的差别。 函数可以具有默认值。 但是,函数调用必须从左到右填写,并且不支持关键字参数。 它们确实支持带有散布参数的随机数量的参数。

To make up for the fact that there are no kwargs, TypeScript implementations often provide an options/props/… last argument, which contains all of the keyword-only arguments. For example, AWS CDK TypeScript has a props argument, while the equivalent Python CDK provides individual keyword arguments.

为了弥补没有kwargs的事实,TypeScript实现通常提供了options/props/…最后一个参数,该参数包含所有仅关键字的参数。 例如, AWS CDK TypeScript有一个props参数,而等效的Python CDK提供了单独的关键字参数。

类和接口 (Classes and interfaces)

Classes are your run of the mill standard OOP classes, while interfaces provide a structure but no implementation.

类是运行Mill标准OOP类的方法,而接口提供结构但不提供实现。

Unlike Python, a class can only extend one other class. A class can implement multiple other classes or interfaces. An interface can only extend another interface, not implement one.

与Python不同,一个类只能扩展另一个类。 一个类可以实现多个其他类或接口。 一个接口只能扩展另一个接口,而不能实现一个接口。

This also shows a nice shorthand in TypeScript. A constructor can define public name: type and the compiler will understand that it has to do this.name = name in the initialisation.

这也显示了TypeScript中的一个不错的速记。 构造函数可以定义public name: type ,编译器将理解它必须执行此操作this.name = name在初始化中。

例外情况 (Exceptions)

Exceptions behave much in the same way in TypeScript as they do in Python. Keyswords differ slightly but most things carry over. TypeScript however does not allow to catch multiple error types at the same time like Python does.

在TypeScript中,异常的行为与在Python中的行为大致相同。 关键字略有不同,但是大多数东西都会保留下来。 但是,TypeScript不允许像Python一样同时捕获多种错误类型。

速记 (Shorthands)

Some great things about modern JavaScript and TypeScript are the shorthands. They help speed your development up a lot, though they are a bit slower than pure implementations with loops etc.

关于现代JavaScript和TypeScript的一些很棒的事情是简写。 尽管它们比带有循环等的纯实现要慢一些,但它们有助于大大加快您的开发速度。

The list includes the basic bool inference like in Python, or short circuiting with a default. But TypeScript has a lot more of these.

该列表包括基本布尔推论(如Python中的推论)或默认情况下的短路。 但是TypeScript有很多这样的功能。

This article provides a more elaborate overview of shorthands, but the ones above covered the most important ones.

本文提供了更详细的速记概述,但是上面的速记涵盖了最重要的速记。

生态系统 (Ecosystem)

Like Python’s pip, TypeScript (and JavaScript) have a package manager called Node Package Manager (npm). It installs all packages locally in a folder node_modules which is infamous for getting very large.

像Python的pip一样,TypeScript(和JavaScript)都有一个名为Node Package Manager(npm)的包管理器 。 它在本地将所有软件包安装在一个文件夹node_modules中 ,这是非常大的名称。

It’s also a common idea that there is really a package for the most trivial stuff in npm. Personally I have the feeling that pip packages are a bit higher quality and better maintained, but that’s just my opinion. There are probably less PyPi packages though.

这也是一个普遍的想法,实际上在npm中有一个用于存储最琐碎内容的程序包。 我个人感觉点子包装质量更高,维护更好,但这只是我的看法。 PyPi软件包可能更少。

The main important libraries are:

主要的重要库是:

  • HTTP calls (like requests): axios, rxjs/ajax, request (deprecated)

    HTTP调用(如请求):axios,rxjs / ajax,请求(不建议使用)
  • Front ends: React vs Angular vs Vue and many, many more

    前端:React,Angular,Vue等等
  • Back ends: Node.js, express (routing)

    后端:Node.js,表达(路由)
  • Time and date: moment.js

    时间和日期:moment.js
  • Reactive data: RxJs.

    React性数据:RxJs。

A bit about RxJs as this is the coolest library I have worked with. It helps you deal with streaming data. It is integral in Angular, where almost all data are observables. It’s a nice way to start learning functional programming.

关于RxJ的一些知识,因为这是我使用过的最酷的库。 它可以帮助您处理流数据。 它是Angular不可或缺的,几乎所有数据都是可观测的。 这是开始学习函数式编程的好方法。

There is also an RxPy library, if you want to work like this in Python.

如果您想在Python中像这样工作,还有一个RxPy库

结论 (Conclusion)

好的 (The good)

  • The TypeScript compiler and typechecker feels much better than mypy. For VS Code it also seems to have better compatability

    TypeScript编译器和类型检查器比mypy好得多。 对于VS Code,它似乎也具有更好的兼容性
  • Null checking is super usefull, especially when working with user data.

    空检查非常有用,尤其是在处理用户数据时。
  • The shorthands can safe a lot of time

    速记可以保证很多时间
  • It’s basically the only language accepted in a browser, and can also be used on a server. This allows for full JS/TS stacks

    它基本上是浏览器唯一接受的语言,也可以在服务器上使用。 这允许完整的JS / TS堆栈
  • I had a lot of fun with RxJS, which forced me to think in a functional way

    我对RxJS感到很开心,这迫使我以一种实用的方式进行思考
  • It’s a very flexible language (like Python), so you can always find a style that suits you

    这是一种非常灵活的语言(例如Python),因此您始终可以找到适合自己的样式
  • A lot of libraries and a huge community

    大量的图书馆和庞大的社区

不好 (The bad)

  • Working with dates is really a pain in the ass, especially coming from datetime and dateutil

    使用日期确实是一个麻烦,尤其是来自datetime和dateutil的时候
  • Running a script is slightly more annoying as you can’t just to python script.py

    运行脚本有点烦人,因为您不仅可以使用python script.py

  • Some of the libraries are not as high quality as some in Python

    一些库的质量不如Python中的库

丑陋的 (The Ugly)

  • It’s still JavaScript

    仍然是JavaScript

  • npm_modules

    npm_modules

  • Error handling is often very confusing, needing to go deep into your stack trace to find anything interesting

    错误处理通常会非常混乱,需要深入堆栈跟踪才能找到有趣的东西
  • Compatability is annoying, as you don’t control the environment. Yes there are still people using IE7. Whether you want to bother making your site run on it, is your decision

    兼容性很烦人,因为您无法控制环境。 是的,仍有人使用IE7。 是否要打扰您的网站,这是您的决定

不同 (The different)

  • As mentioned, the style is different. It leans much more towards functional programming than OOP. It takes some getting used to

    如前所述,样式是不同的。 它比OOP更倾向于函数式编程。 需要一些时间来适应
  • People actually install a library for any trivial function

    人们实际上为任何琐碎的功能安装了一个库

There is obviously a lot I did not mention. But as experienced developers, you know that the answer is one Google search or StackExchange solution away.

显然我没有提到很多。 但是,作为经验丰富的开发人员,您知道答案是一个Google搜索或StackExchange解决方案。

Message me if you have any questions, or ask them in the comments. Thank you for reading my first technical Medium article!

如果您有任何问题,请给我发消息,或者在评论中提问。 感谢您阅读我的第一篇技术性中型文章!

翻译自: https://medium.com/analytics-vidhya/typescript-for-python-developers-a16e50a5acb2

python 打字游戏

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值