我为什么建议前端将Python 作为第二语言?

2022-10-12,,,

前言

本文的文字及图片来源于网络,仅供学习、交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理。

作者: 前端劝退师

ps:如有需要python学习资料的小伙伴可以加点击下方链接自行获取

1. python和es6语法差别

基本类型

值得注意的是,尽管两者都是动态类型,但python连接时并不会自动转换类型。

1 // javascript
2 let coerced = 1;
3 let concatenated = coerced + 'string';

 

1 # python
2 not_coerced = 1
3 concatenated = not_coerced + 'string'

 

直接报错:typeerror: cannot concatenate 'str' and 'int' objects

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

1 # python
2 not_coerced = 1
3 concatenated = str(not_coerced) + 'string'

 

2. functions ormethods?

在javascript和python中,函数和条件的结构极为相似。例如:

 1 // javascript
 2 function drseuss(catinthehat, thing1, thing2) {
 3   if (catinthehat == true &&
 4     thing1 == true &&
 5     thing2 == true) {
 6     console.log('is cray');
 7   } else if (catinthehat != true) {
 8     console.log('boring');
 9   } else {
10     console.log('so boring');
11   }
12 }

 

 1 # python
 2 def dr_seuss(cat_in_the_hat, thing1, thing2):
 3   if cat_in_the_hat == true and
 4     thing2 == true and
 5     thing2 == true:
 6     print 'is cray'
 7   elif cat_in_the_hat != true:
 8     print 'boring'
 9   else:
10     print 'so boring'

 

但在javascript中,“methods”的通俗定义是指语言规范中内置的方法,例如:function.prototype.apply() mdn上有对二者的解释: 在大多数方面,functionsmethods相同,但有两个主要区别:

  • methods可以被隐式传递到调用该methods的对象上。

  • methods能够对类中包含的数据进行操作。

然鹅,在javascript中,“类”只是语法糖的存在,稍后我们再进行对比。

3. 模板字符串

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

1 // javascript
2 let exclamation = 'whoa!';
3 let sentence = `they are really similar to python.`;
4  
5 console.log(`template literals: ${exclamation} ${sentence}`);

 

1 # python
2 print '打印: {} {}'.format('whoa.', 'quite!')
3 # 打印: yup. quite!

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

直接对比:

1 name = "tom"
2 age = 3
3 print(f"他叫 {name}, {age} 岁")
4 # "他叫tom, 3 岁"

 

4. 参数默认值

javascript再次完美“借鉴”python:

1 // javascript
2 function nom(food="ice cream") {
3   console.log(`time to eat ${food}`);
4 }
5  
6 nom();// time to eat ice cream

 

1 # python
2 def nom(food="ice cream"):
3   print 'time to eat {}'.format(food)
4  
5 nom() # time to eat ice cream

 

5. 其余参数和* args

rest参数语法,使我们可以将不定数量的参数表示为数组,传入函数中。

  • 在python中,它们称为* args

  • 在javascript中...xxx就表示为其余参数。

 1 // javascript
 2 function joke(question, ...phrases) {
 3   console.log(question);
 4   for (let i = 0; i > phrases.length; i++) {
 5     console.log(phrases[i]);
 6   }
 7 }
 8 
 9 let es6joke = "why does js single out one parameter?"
10 joke(es6joke, "because it doesn't", 'really like', 'all the rest of them!');
11  
12 // why does js single out one parameter?
13 // because it doesn't
14 // really like
15 // all the rest of them!

 

 1 # python
 2 def pirate_joke(question, *args):
 3   print question
 4   for arg in args:
 5     print arg
 6  
 7 python_joke = "what's a pyrate's favorite parameter?"
 8  
 9 pirate_joke(python_joke, "*args!", "*arrgs!", "*arrrgs!")
10  
11 # what's a pyrate's favorite parameter?
12 # *args!
13 # *arrgs!
14 # *arrrgs!

 

6. classes:类

众所周知,es6类实际上是语法糖。 python具有内置的类,可以快速,轻松地进行面向对象的编程。

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

 1 // javascript
 2 class mammal {
 3   constructor() {
 4     this.neocortex = true;
 5   }
 6 }
 7  
 8 class cat extends mammal {
 9   constructor(name, years) {
10     super();
11     this.name = name;
12     this.years = years;
13   }
14  
15   eat(food) {
16     console.log('nom ' + food);
17   }
18 }

 

 1 # python
 2 class mammal(object):
 3   neo_cortex = true
 4  
 5 class cat(mammal):
 6   def __init__(self, name, years):
 7     self.name = name
 8     self.years = years
 9  
10   def eat(food):
11     print 'nom %s' % (food)
12  
13 fry_cat = cat('fry', 7)
14 fry_cat.eat('steak')

 

心而论,python的写法更优雅。。。

7. modules and import:模块

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

  • javascript导入是静态的;python是动态的。

  • javascript模块必须显式导出。在python中,所有模块均可导入。

  • javascript具有默认导出的概念。python没有。

1 # python
2 import mymodule
3 mymodule.myfunc()

 

1 // javascript
2 import * as myalias from "./mymodule";
3 myalias.myfunc();

1. 导入分模块

在javascript中,我们想导入分模块直接解构赋值就可以了

1 // javascript
2 import { myvar, myfunc } from "./mymodule";
3 console.log(myvar);
4 myfunc();

而在python,其语义则相反:

1 # python
2 from mymodule import myvar, myfunc
3 print myvar
4 myfunc()

2. 导出空函数

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

# python
def myfunc(): pass

// javascript
export function myfunc() {}

前端如何优雅学会python?

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

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

网络爬虫是python比较常用的一个场景,国际上,google在早期大量地使用python语言作为网络爬虫的基础,带动了整个python语言的应用发展。

就我个人发展而已,我也十分推荐以爬虫为应用入门,原因有几项:

  • 爬虫是针对web页面的一种应用技术,前端可以无痛衔接很多知识。

  • 爬虫的第一步是获取页面源码,然后做信息抽取。其中针对dome节点的class/id选择,前端无需再度学习。

  • 爬虫中的虚拟登录及selenium,可以提升前端对于自动化测试的理解。

  • 爬虫的最终形态是搜索引擎,当中的seo是每个前端都需要关注的点儿。

  • 在了解搜索引擎爬虫的过程中,前端可以搞清楚服务端渲染ssr和单页应用csr的不同作用。

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

  • 面向页面,前端自然轻车熟路。

  • 面向接口,需要了解到如何用抓包软件(fiddler/charles)。

  • 在这过程中,又能学会一项技能 - 抓包。以后不用再看着network傻傻刷新了。

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

爬虫—> 数据清洗 -> 数据库操作 -> 数据清洗 -> 数据挖掘 -> 数据分析 ...

这一条链下去,你可以学到非常非常多的知识:

scrapy爬虫框架,redis分布式事务,数据处理pandas,自然语言分析nlp,完整实现数据可视化等等....

关于语言的讨论,我非常赞同李兵老师的这段话:

3. 潘石屹都在学python

.

《我为什么建议前端将Python 作为第二语言?.doc》

下载本文的Word格式文档,以方便收藏与打印。