前言

“如何解脱不停切图的困局?”

这不是一篇制作焦虑的文章,而是充斥真挚倡议的Python推广文。

当议论到编程入门语言时,大多数都会举荐PythonJavaScript

实际上,两种语言在方方面面都十分弱小。

而现在咱们熟知的ES6语言,很多语法都是借鉴Python的。

有一种说法是 “能用js实现的,最初肯定都会用js实现。”

那么这里能够说:“能跟python长得像的,最初肯定会像python。”

1. PythonES6语法差异

1. 根本类型

值得注意的是,只管两者都是动静类型,但python连贯时并不会主动转换类型。

// JavaScriptlet coerced = 1;let concatenated = coerced + 'string';
// Pythonnot_coerced = 1concatenated = not_coerced + 'string'

间接报错:TypeError: cannot concatenate 'str' and 'int' objects

只有提前把num转换为字符串类型能力正确运行

# Pythonnot_coerced = 1concatenated = str(not_coerced) + 'string'

2. Functions ormethods?

JavaScriptPython中,函数和条件的构造极为类似。例如:

// JavaScriptfunction drSeuss(catInTheHat, thing1, thing2) {  if (catInTheHat == true &&    thing1 == true &&    thing2 == true) {    console.log('is cray');  } else if (catInTheHat != true) {    console.log('boring');  } else {    console.log('so boring');  }}
# Pythondef dr_seuss(cat_in_the_hat, thing1, thing2):  if cat_in_the_hat == True and    thing2 == True and    thing2 == True:    print 'is cray'  elif cat_in_the_hat != True:    print 'boring'  else:    print 'so boring'

但在JavaScript中,“methods”的艰深定义是指语言标准中内置的办法,例如:Function.prototype.apply()

MDN上有对二者的解释:

在大多数方面,Functionsmethods雷同,但有两个次要区别:

  • methods能够被隐式传递到调用该methods的对象上。
  • methods可能对类中蕴含的数据进行操作。

然鹅,在JavaScript中,“类”只是语法糖的存在,稍后咱们再进行比照。

3. 模板字符串

在模板字符串上,JavaScript之前是当先于python的。

// JavaScriptlet exclamation = 'Whoa!';let sentence = `They are really similar to Python.`; console.log(`Template Literals: ${exclamation} ${sentence}`);
# pythonprint '打印: {} {}'.format('Whoa.', 'Quite!')# 打印: Yup. Quite!

{}充当占位符。 这种语法被诟病颇多,于是在起初的Python3.6版本中,又提供了一种字符串格式化语法——f-strings

间接比照:

name = "Tom"age = 3print(f"他叫 {name}, {age} 岁")# "他叫Tom, 3 岁"

4. 参数默认值

JavaScript再次完满“借鉴”Python:

// JavaScriptfunction nom(food="ice cream") {  console.log(`Time to eat ${food}`);} nom();// Time to eat ice cream
# Pythondef nom(food="ice cream"):  print 'Time to eat {}'.format(food) nom() # Time to eat ice cream

5. 其余参数和* args

Rest参数语法,使咱们能够将不定数量的参数示意为数组,传入函数中。

  • Python中,它们称为* args
  • JavaScript...xxx就示意为其余参数。
// JavaScriptfunction joke(question, ...phrases) {  console.log(question);  for (let i = 0; i > phrases.length; i++) {    console.log(phrases[i]);  }}let es6Joke = "Why does JS single out one parameter?"joke(es6Joke, "Because it doesn't", 'really like', 'all the REST of them!'); // Why does JS single out one parameter?// Because it doesn't// really like// all the REST of them!
# Pythondef pirate_joke(question, *args):  print question  for arg in args:    print arg python_joke = "What's a Pyrate's favorite parameter?" pirate_joke(python_joke, "*args!", "*arrgs!", "*arrrgs!") # What's a Pyrate's favorite parameter?# *args!# *arrgs!# *arrrgs!

6. Classes:类

家喻户晓,ES6类实际上是语法糖。 Python具备内置的类,能够疾速,轻松地进行面向对象的编程。

JavaScript原型链继承,是每个前端的必须课。

// JavaScriptclass Mammal {  constructor() {    this.neocortex = true;  }} class Cat extends Mammal {  constructor(name, years) {    super();    this.name = name;    this.years = years;  }   eat(food) {    console.log('nom ' + food);  }}
# Pythonclass Mammal(object):  neo_cortex = True class Cat(Mammal):  def __init__(self, name, years):    self.name = name    self.years = years   def eat(food):    print 'nom %s' % (food) fry_cat = Cat('Fry', 7)fry_cat.eat('steak')

平心而论,Python的写法更优雅。。。

7. Modules and import:模块

ES6的模块语言借鉴于python,却优良于它。 两者之间有一些区别:

  1. JavaScript导入是动态的;Python是动静的。
  2. JavaScript模块必须显式导出。在Python中,所有模块均可导入。
  3. JavaScript具备默认导出的概念。Python没有。
# pythonimport mymodulemymodule.myfunc()
// javascriptimport * as myalias from "./mymodule";myalias.myfunc();

1. 导入分模块

javascript中,咱们想导入分模块间接解构赋值就能够了

// javascriptimport { myvar, myfunc } from "./mymodule";console.log(myvar);myfunc();

而在python,其语义则相同:

# pythonfrom mymodule import myvar, myfuncprint myvarmyfunc()

2. 导出空函数

如何想导出一段空函数,python须要用到“pass“关键词占位,防止运行出错。 mymodule.py:

# pythondef myfunc(): pass// javascriptexport function myfunc() {}

更多具体比照能够看这篇: Modules and import in ES6 for Python developers

2. 前端如何优雅学会Python

许多前端对Python的激情始于好奇,终于停滞。

间隔实干做开发有技术差距,也无人指导提带,也不知当下程度能干嘛?就在这样的纳闷循环中,编程技能止步不前,而爬虫是最好的进阶方向之一。

网络爬虫是Python比拟罕用的一个场景,国内上,google在晚期大量地应用Python语言作为网络爬虫的根底,带动了整个Python语言的利用倒退。

就我集体倒退而已,我也非常举荐以爬虫为利用入门,起因有几项:

  • 爬虫是针对web页面的一种利用技术,前端能够无痛连接很多常识。
  • 爬虫的第一步是获取页面源码,而后做信息抽取。其中针对dome节点的class/id抉择,前端无需再度学习。

  • 爬虫中的虚构登录及Selenium,能够晋升前端对于自动化测试的了解。
  • 爬虫的最终状态是搜索引擎,当中的SEO是每个前端都须要关注的点儿。
  • 在理解搜索引擎爬虫的过程中,前端能够搞清楚服务端渲染SSR和单页利用CSR的不同作用。

爬虫分两种形式:面向页面和面向接口

  • 面向页面,前端天然驾轻就熟。
  • 面向接口,须要理解到如何用抓包软件(Fiddler/Charles)。
  • 在这过程中,又能学会一项技能 - 抓包。当前不必再看着Network傻傻刷新了。

始于爬虫,却不止于爬虫:

爬虫—> 数据荡涤 -> 数据库操作 -> 数据荡涤 -> 数据挖掘 -> 数据分析 ...

这一条链上来,你能够学到十分十分多的常识:

Scrapy爬虫框架,Redis分布式事务,数据处理Pandas,自然语言剖析NLP,残缺实现数据可视化等等....

对于语言的探讨,我十分同意李兵老师的这段话:

3. 潘石屹都在学Python

本文转自 https://juejin.cn/post/6844904004154064910,如有侵权,请分割删除。