bl双性强迫侵犯h_国产在线观看人成激情视频_蜜芽188_被诱拐的少孩全彩啪啪漫画

Python3有什么新特性-創(chuàng)新互聯(lián)

這篇文章主要介紹Python3有什么新特性,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!

創(chuàng)新互聯(lián)長期為成百上千客戶提供的網(wǎng)站建設服務,團隊從業(yè)經(jīng)驗10年,關注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務;打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為仙居企業(yè)提供專業(yè)的網(wǎng)站建設、成都網(wǎng)站設計,仙居網(wǎng)站改版等技術服務。擁有10余年豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。

概述

到2020年,Python2的官方維護期就要結(jié)束了,越來越多的Python項目從Python2切換到了Python3。其實在實際工作中,很多伙伴都還是在用Python2的思維寫Python3的代碼。給大家總結(jié)一下Python3一些新的更方便的特性!希望你們看完后也能高效率的編寫代碼

f-strings (3.6+)

在Python里面,我們經(jīng)常使用format函數(shù)來格式化字符串,例如:

user = "Jane Doe"action = "buy"log_message = 'User {} has logged in and did an action {}.'.format(
 user,
 action)print(log_message)輸出:User Jane Doe has logged in and did an action buy.

Python3里面提供了一個更加靈活方便的方法來格式化字符串,叫做f-strings。上面的代碼可以這樣實現(xiàn):

user = "Jane Doe"action = "buy"log_message = f'User {user} has logged in and did an action {action}.'print(log_message)輸出: User Jane Doe has logged in and did an action buy.

Pathlib (3.4+)

f-strings這個功能太方便了,但是對于文件路勁這樣的字符串,Python還提供了更加方便的處理方法。Pathlib是Python3提供的一個處理文件路勁的庫。例如:

from pathlib import Pathroot = Path('post_sub_folder')print(root)輸出結(jié)果: post_sub_folder
path = root / 'happy_user'# 輸出絕對路勁print(path.resolve())輸出結(jié)果:/root/post_sub_folder/happy_user

Type hinting (3.5+)

靜態(tài)與動態(tài)類型是軟件工程中的一個熱門話題,每個人都有不同的看法,Python作為一個動態(tài)類型語言,在Python3中也提供了Type hinting功能,例如:

def sentence_has_animal(sentence: str) -> bool:
 return "animal" in sentence
sentence_has_animal("Donald had a farm without animals")# True

Enumerations (3.4+)

Python3提供的Enum類讓你很容就能實現(xiàn)一個枚舉類型:

from enum import Enum, autoclass Monster(Enum):
  ZOMBIE = auto()
  WARRIOR = auto()
  BEAR = auto()print(Monster.ZOMBIE)輸出: Monster.ZOMBIE

Python3的Enum還支持比較和迭代。

for monster in Monster:
  print(monster)輸出: Monster.ZOMBIE   Monster.WARRIOR   Monster.BEAR

Built-in LRU cache (3.2+)

緩存是現(xiàn)在的軟件領域經(jīng)常使用的技術,Python3提供了一個lru_cache裝飾器,來讓你更好的使用緩存。下面有個實例:

import timedef fib(number: int) -> int:
  if number == 0: return 0
  if number == 1: return 1
  return fib(number-1) + fib(number-2)start = time.time()fib(40)print(f'Duration: {time.time() - start}s')# Duration: 30.684099674224854s

現(xiàn)在我們可以使用lru_cache來優(yōu)化我們上面的代碼,降低代碼執(zhí)行時間。

from functools import lru_cache@lru_cache(maxsize=512)def fib_memoization(number: int) -> int:
  if number == 0: return 0
  if number == 1: return 1
  return fib_memoization(number-1) + fib_memoization(number-2)start = time.time()fib_memoization(40)print(f'Duration: {time.time() - start}s')# Duration: 6.866455078125e-05s

Extended iterable unpacking (3.0+)

代碼如下:

head, *body, tail = range(5)print(head, body, tail)輸出: 0 [1, 2, 3] 4py, filename, *cmds = "python3.7 script.py -n 5 -l 15".split()print(py)print(filename)print(cmds)輸出:python3.7
   script.py   ['-n', '5', '-l', '15']first, _, third, *_ = range(10)print(first, third)輸出: 0 2

Data classes (3.7+)

Python3提供data class裝飾器來讓我們更好的處理數(shù)據(jù)對象,而不用去實現(xiàn) init () 和 repr() 方法。假設如下的代碼:

class Armor:
  def __init__(self, armor: float, description: str, level: int = 1):
    self.armor = armor    self.level = level    self.description = description  def power(self) -> float:
    return self.armor * self.level
armor = Armor(5.2, "Common armor.", 2)armor.power()# 10.4print(armor)# <__main__.Armor object at 0x7fc4800e2cf8>

使用data class實現(xiàn)上面功能的代碼,這么寫:

from dataclasses import dataclass@dataclassclass Armor:
  armor: float
  description: str
  level: int = 1
  def power(self) -> float:
    return self.armor * self.level
armor = Armor(5.2, "Common armor.", 2)armor.power()# 10.4print(armor)# Armor(armor=5.2, description='Common armor.', level=2)

Implicit namespace packages (3.3+)

通常情況下,Python通過把代碼打成包(在目錄中加入 init .py實現(xiàn))來復用,官方給的示例如下:

sound/             Top-level package
   __init__.py        Initialize the sound package
   formats/         Subpackage for file format conversions
       __init__.py
       wavread.py
       wavwrite.py
       aiffread.py
       aiffwrite.py
       auread.py
       auwrite.py       ...
   effects/         Subpackage for sound effects
       __init__.py
       echo.py
       surround.py
       reverse.py       ...
   filters/         Subpackage for filters
       __init__.py
       equalizer.py
       vocoder.py
       karaoke.py

在Python2里,如上的目錄結(jié)構,每個目錄都必須有 init .py文件,一遍其他模塊調(diào)用目錄下的python代碼,在Python3里,通過 Implicit Namespace Packages可是不使用__init__.py文件

sound/             Top-level package
   __init__.py        Initialize the sound package
   formats/         Subpackage for file format conversions
       wavread.py
       wavwrite.py
       aiffread.py
       aiffwrite.py
       auread.py
       auwrite.py       ...
   effects/         Subpackage for sound effects
       echo.py
       surround.py
       reverse.py       ...
   filters/         Subpackage for filters
       equalizer.py
       vocoder.py
       karaoke.py

以上是“Python3有什么新特性”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關知識,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

本文標題:Python3有什么新特性-創(chuàng)新互聯(lián)
網(wǎng)頁地址:http://vcdvsql.cn/article16/cedegg.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站維護響應式網(wǎng)站網(wǎng)站策劃品牌網(wǎng)站設計App開發(fā)網(wǎng)站改版

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

營銷型網(wǎng)站建設