最全妙不可言。写出优雅的 Python 代码的七条重要技巧,2024年最新被面试官怼了还有戏吗

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

print(“良好”)

return

if score >= 70: # 中等

print(“中等”)

return

if score >= 60: # 及格

print(“及格”)

return

print(“不及格”)

print(“程序结束”)

这种处理方式是极其优雅的,从上往下清晰明了,大大增加了代码的可读性和可维护性。

0x06 生成器


我们都知道通过列表生成式可以直接创建一个新的列表,但受机器内存限制,列表的容量肯定是有限的。如果列表里面的数据是通过某种规律推导计算出来的,那是否可以在迭代过程中不断的推算出后面的元素呢,这样就不必一次性创建完整个列表,按需使用即可,这时候生成器就派上用场了。

0x07 装饰器


试想一下如下的场景,当后端接收到用户请求后,需要对用户进行鉴权,总不能将鉴权的代码复制来复制去吧;还有我们的项目都是需要记录日志的,这两种情况最适合使用装饰器。事实上 Flask 框架中就大量使用装饰器来进行鉴权操作。

一切皆对象!

在 Python 中我们可以在函数中定义函数,也可以从函数中返回函数,还可以将函数作为参数传给另一个函数。

def hi(name=“yasoob”):

print(“now you are inside the hi() function”)

def greet():

return “now you are in the greet() function”

def welcome():

return “now you are in the welcome() function”

print(greet())

print(welcome())

print(“now you are back in the hi() function”)

hi()

output

now you are inside the hi() function

now you are in the greet() function

now you are in the welcome() function

now you are back in the hi() function

在上面的代码中,我们在 hi() 函数内部定义了两个新的函数,无论何时调用 hi() 其内部的函数都将会被调用。

def hi(name=“yasoob”):

def greet():

return “now you are in the greet() function”

def welcome():

return “now you are in the welcome() function”

if name == “yasoob”:

return greet

else:

return welcome

a = hi()

print(a)

print(a())

output

<function hi..greet at 0x7fe3e547a0e0>

now you are in the greet() function

在这个例子中,由于默认参数 name = yasoob 因此 a = hi() 返回的是 greet 函数。a 也就指向了 hi() 函数内部的 greet() 函数。

def hi():

return “hi yasoob!”

def doSomethingBeforeHi(func):

print(“I am doing some boring work before executing hi()”)

print(func())

doSomethingBeforeHi(hi)

output

I am doing some boring work before executing hi()

hi yasoob!

在最后这个例子中,我们将 hi() 函数传递给了另外一个函数,并且他们还很愉快的执行了。

现在,让我们来看看 Python 中的装饰器吧。

def a_new_decorator(a_func):

def wrapTheFunction():

print(“I am doing some boring work before executing a_func()”)

a_func()

print(“I am doing some boring work after executing a_func()”)

return wrapTheFunction

def a_function_requiring_decoration():

print(“I am the function which needs some decoration to remove my foul smell”)

a_new_function_requiring_decoration = a_new_decorator(a_function_requiring_decoration)

a_new_function_requiring_decoration()

output

I am doing some boring work before executing a_func()

I am the function which needs some decoration to remove my foul smell

I am doing some boring work after executing a_func()

看懂了没,就是上面我们介绍的基础操作的组合。事实上这就是 python 中的装饰器所做的事,通过这种方式来修改一个函数的行为。

但如果每次都这么写的话未免也太麻烦了吧,因此 python 为我们提供了一个便捷操作 @

def a_new_decorator(a_func):

@a_new_decorator

def a_function_requiring_decoration():

print(“I am the function which needs some decoration to remove my foul smell”)

a_function_requiring_decoration()

output

I am doing some boring work before executing a_func()

I am the function which needs some decoration to remove my foul smell

I am doing some boring work after executing a_func()

总结

今天我给大家介绍了几个重要的提升代码逼格的技巧,小伙伴们还有什么独家技巧可以在评论区交流哦~

最后

不知道你们用的什么环境,我一般都是用的Python3.6环境和pycharm解释器,没有软件,或者没有资料,没人解答问题,都可以免费领取(包括今天的代码),过几天我还会做个视频教程出来,有需要也可以领取~

给大家准备的学习资料包括但不限于:

Python 环境、pycharm编辑器/永久激活/翻译插件

python 零基础视频教程

Python 界面开发实战教程

Python 爬虫实战教程

Python 数据分析实战教程

python 游戏开发实战教程

Python 电子书100本

Python 学习路线规划

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

  • 14
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值