资讯专栏INFORMATION COLUMN

Do you want to be a Python expert ? 前言

孙淑建 / 832人阅读

摘要:很多时候有些人在介绍的时候会提到但是我不知道你需要多久才能做到中说的真的优雅吗真的简洁吗这是当然不然怎么会添加到标准库中去不过在此之前你需要更加的学习毕竟不是一上来就什么都会的明白的风格或者说需要自己不断锻炼让自己写出来的代码更

Do you want to be a Python expert ?

https://github.com/ltoddy/Pyt...

很多时候,有些人在介绍 Python 的时候会提到 The Zen of Python :

>>> import this
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren"t special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you"re Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it"s a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let"s do more of those!

但是我不知道你需要多久才能做到 The Zen of Python 中说的.

Python 真的优雅吗, Python 真的简洁吗, 这是当然, 不然 The Zen of Python 怎么会添加到标准库中去.

不过在此之前,你需要更加的学习(毕竟不是一上来就什么都会的), 明白 Python 的风格, 或者说需要自己不断锻炼, 让自己写出来的 Python 代码更加的 pythonic.

在这里, 我不会讲述类似:

a, b = b, a

l = [x * 2 for x in range(10)]

类似这种你本就应该在初学 Python 就应该熟练掌握的东西.

我更想讲述的是:

data model class (dunder methods, protocol).

metaclass (Base/Derive class)

Decorators

Generators

Context Managers

因为以上的一些feature确实可以让你的代码更加pythonic, 而且也是非常重要的.


A problem

在很早很早之前,曾经看到一个群友问了一个问题:

有两个list, 比如: one = [1, 2, 3], other = [2, 3, 4]

他想要得到这样的结果,把这两个相加得到: [3, 5, 7], 也就是对应下标的元素相加.

我当时想都没想就回了一句: return [one[i] + other[i] for i in range(len())]

看上去不错,是吗?

当然不是. 如果这两个list不相等怎么办?

然后我又给了一个方案: return list(map(lambda x, y: x + y, one, other))

不过, 这样又不好了, 如果一个list长,一个list短,这个样子写,长的那个list多出来的数据就会被丢掉了.

所以我又思考了一下, 重新给出了最后结果: return list(starmap(lambda x, y: x + y, zip_longest(one, ther, fillvalue=0)))

你能想到我所说的最后一种方案吗?


What is Python ?

Python 到底是一种什么样的语言, 说真的, 很难给 Python 下一个定义,因为它的范式实在太多:

面向对象

过程式

面向协议

原型

也支持函数式的feature

Python is an interpreted, interactive, object-oriented programming language. (来自Python docs)

如果你真正理解这些 feature 是什么, 那么非常显然的是, 来告诉我: why and when you use it. (毕竟我更倾向于实用, 我不是学院派)


当然,在最后的时候,我也会告诉一些学习途径, 比如看什么书可以最快的提高你的能力, 也比如你应该从哪些地方去获取你需要掌握的知识.

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/42515.html

相关文章

  • Python-Socket-Programming(1)

    摘要: Abstract Sockets are used nearly everywhere, but are one of the most severely misunderstood technologies around. This is a 10,000 foot overview of sockets. It’s not really a tutorial - you’ll s...

    animabear 评论0 收藏0
  • Python相对导入导致SystemError的解决方案(译)

    摘要:相对导入相对导入使用模块的决定它是否在一个包内。当你是用类似进行相对导入的时候,点表明在包的层次中上升多少。所以,你不能在交互式会话中直接使用任何相对导入。 源题目与答案地址如下http://stackoverflow.com/questions/14132...。下面是我的翻译(看作机翻也行),以及原文。 这个问题是如何解决在相对导入的时候,如果出现System Error的时候的解...

    ethernet 评论0 收藏0
  • 打造数据科学作品集:搭建一个数据科学博客

    摘要:读完本文,你将学会如何使用静态网站生成器,搭建一个属于自己的博客,用来展示数据科学作品。静态网站基本上,一个静态网站就是一个全是文件的文件夹。建立静态网站的一种方法是手写,然后上传所有的文件到服务器。 这是「打造数据科学作品集」系列文章的第二篇。如果你喜欢该系列,而且想知道下一篇文章什么时候发布,你可以订阅我们。读完本文,你将学会如何使用 Pelican 静态网站生成器,搭建一个属于...

    iamyoung001 评论0 收藏0
  • How did I install Youcompleteme

    I always want imporve my skills on VIM. Thus recently I spent two days on setting up a VIM IDE for C/C++ proprogramming. Below are some useful steps for YCM. Specifications Mac InfoOS X Yosemite Versi...

    TalkingData 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<