1. 程式人生 > >Python 3.7 runtime now available in AWS Lambda

Python 3.7 runtime now available in AWS Lambda

This post is courtesy of Shivansh Singh, Partner Solutions Architect – AWS

We are excited to announce that you can now develop your AWS Lambda functions using the Python 3.7 runtime. Start using this new version today by specifying a runtime parameter value of “python3.7″ when creating or updating functions. AWS continues to support creating new Lambda functions on Python 3.6 and Python 2.7.

Here’s a quick primer on some of the major new features in Python 3.7:

  • Data classes
  • Customization of access to module attributes
  • Typing enhancements
  • Time functions with nanosecond resolution

Data classes

In object-oriented programming, if you have to create a class, it looks like the following in Python 3.6:

class Employee:
    def __init__(self, name: str, dept: int) -> None:
        self.name = name
        self.dept = dept
        
    def is_fulltime(self) -> bool:
        """Return True if employee is a Full-time employee, else False"""
        return self.dept > 25

The __init__

method receives multiple arguments to initialize a call. These arguments are set as class instance attributes.

With Python 3.7, you have dataclasses, which make class declarations easier and more readable. You can use the @dataclass decorator on the class declaration and self-assignment is taken care of automatically. It generates __init__, __repr__, __eq__, __hash__, and other special methods. In Python 3.7, the Employee class defined earlier looks like the following:

@dataclass
Class Employee:
    name: str
    dept: int
    
    def is_fulltime(self) -> bool:
        """Return True if employee is a Full-time employee, else False"""
        return self.dept > 25

Customization of access to module attributes

Attributes are widely used in Python. Most commonly, they used in classes. However, attributes can be put on functions and modules as well. Attributes are retrieved using the dot notation: something.attribute. You can also get attributes that are named at runtime using the getattr() function.

For classes, something.attr first looks for attr defined on something. If it’s not found, then the special method something.__getattr__(“attr”) is called. The .getattr__() function can be used to customize access to attributes on objects. This customization is not easily available for module attributes, until Python 3.7. But PEP 562 provides __getattr__() on modules, along with a corresponding __dir__() function.

Typing enhancements

Type annotations are commonly used for code hints. However, there were two common issues with using type hints extensively in the code:

  • Annotations could only use names that were already available in the current scope. In other words, they didn’t support forward references.
  • Annotating source code had adverse effects on the startup time of Python programs.

Both of these issues are fixed in Python 3.7, by postponing the evaluation of annotations. Instead of compiling code that executes expressions in annotations at their definition time, the compiler stores the annotation in a string form equivalent to the AST of the expression in question.

For example, the following code fails, as spouse cannot be defined as type Employee, given that Employee is not defined yet.

class Employee:
    def __init__(self, name: str, spouse: Employee) --> None
        pass

In Python 3.7, the evaluation of annotation is postponed. It gets stored as a string and optionally evaluated as needed. You do need to import __future__, which breaks the backward compatibility of the code with previous versions of Python.

from __future__ import annotations

class Employee:
    def __init__(self, name: str, spouse: Employee) --> None
        pass

Time functions with nanosecond resolution

The time module gets some new functions in Python 3.7. The clock resolution can exceed the limited precision of a floating point number returned by the time.time() function and its variants. The following new functions are being added:

  • clock_gettime_ns()
  • clock_settime_ns()
  • monotonic_ns()
  • perf_counter_ns()
  • process_time_ns()
  • time_ns()

These functions are similar to already existing functions without the _ns suffix. The difference is that the above functions return a number of nanoseconds as an int instead of a number of seconds as a float.

Hope you enjoy… go build with Python 3.7!

相關推薦

Python 3.7 runtime now available in AWS Lambda

This post is courtesy of Shivansh Singh, Partner Solutions Architect – AWS We are excited to announce that you can now develop your AWS La

Python 3.7 now available!

If you signed up since 28 August, you'll have Python 3.7 available on your account -- you can use it just like any other Python version. If you signed

PyTorch 1.0 preview now available in Amazon SageMaker and the AWS Deep Learning AMIs

Amazon SageMaker and the AWS Deep Learning AMIs (DLAMI) now provide an easy way to evaluate the PyTorch 1.0 preview release. PyTorch 1.0 adds seam

The Ultimate Guide to Data Classes in Python 3.7

One new and exciting feature coming in Python 3.7 is the data class. A data class is a class typically containing mainly data, although there aren’t rea

Cool New Features in Python 3.7

Python 3.7 is officially released! This new Python version has been in development since September 2016, and now we all get to enjoy the results of the

AWS Educate Now Available in Spanish and Portuguese

Spanish and Portuguese text to follow The AWS Educate program has been translated into Spanish and Portuguese, expanding the numb

零基礎學python-3.7 還有一個程序 python讀寫文本

efi == put ret mode nbsp inpu exce for each 今天我們引入另外一個程序,文件的讀寫 我們先把簡單的程序代碼貼上。然後通過我們多次的改進。希望最後可以變成一個簡單的文本編輯器 以下是我們最簡單的代碼: ‘crudfile--讀寫文

安裝Python-3.7和linux下網速測試工具

網速測試工具 Python-3.7 wget https://www.python.org/ftp/python/3.7.0/Python-3.7.0b4.tar.xz tar -xvJf Python-3.7.0b4.tar.xz cd Python-3.7.0b4/ ./configure --p

阿裏雲PyODPS 0.7.18發布,針對聚合函數進行優化同時新增對Python 3.7支持

click tiny alt and gin 上傳下載 note 更多 依賴 摘要: PyODPS是MaxCompute的Python版本的SDK,SDK的意思非常廣泛,輔助開發某一類軟件的相關文檔、範例和工具的集合都可以叫做“SDK”。 PyODPS在這裏的作用是提供了對

安裝完Anaconda python 3.7,想使用python3.6方法

版本 python3.6 user this anaconda active create class dea cmd使用命令:conda create -n py36 python=3.6 anaconda安裝好後,會有提示: To activate this envir

[雪峰磁針石部落格]python 3.7極速入門教程2 Hello與變數

Hello 命令列方式 $ python Python 3.7.0 (default, Jun 28 2018, 13:15:42) [GCC 7.2.0] :: Anaconda, Inc. on linux Type "help", "copyright", "credits" or "license

C++呼叫Python 3.7.0

開發環境:Visual Studio 2015 + Python 3.7.0 一、在C++程式碼中寫Python指令碼 在C++中含有呼叫Python指令碼的API函式,在C++程式碼中嵌入Python指令碼程式碼,是通過PyRun_SimpleString()函式實現。它允許將Pyth

Python 3.7極速入門教程9最佳python中文書籍下載

篩選了2年內優秀的python書籍,個別經典的書籍擴充套件到5年內。 儘量只收集通用的書籍,專用的書籍只收集特別優秀者。 python專業書籍彙總 2018最佳人工智慧影象處理工具OpenCV書籍下載 Python基礎教程(第3版) - 2017.pdf &

2018 - Python 3.7 爬蟲之 利用 Scrapy 框架 獲取圖片並下載(二)

一、 通過命令構建一個爬蟲專案 二、定義 item 三、啟用 pipeline 管道 四、編寫爬蟲 Spider 五、執行爬蟲 六、結果檢視 未安裝 Scrapy 框架,見上一篇文章:框架安裝及配置 一、 通過命令構建一個爬蟲專

2018 - Python 3.7 爬蟲之 Scrapy 框架的安裝及配置(一)

一,安裝 Python3.7 二,安裝 pip 三,安裝 pywin32 四,安裝 pyOpenSSL 五,安裝 lxml 六,安裝 zope.interface 七,安裝 twisted 八,安裝 Scrapy 九,一鍵升級所有庫,Python 3.7親測可用,建立

Ubuntu 18 安裝 Python 3.7 報錯的解決方案

Ubuntu 18 安裝 Python 3.7 報錯的解決方案 Ubuntu 18 安裝 Python 3.7 報錯的解決方案 0. 環境 1. sudo make install 報錯 ZipImportEr

mac os, python 3.7 安裝opencv3

感想 programmer在做事情的時候,最厭煩的就是安裝這個安裝那個了,我也是,然後我發現安裝opencv到python3也不是很容易,我這裡分享一下我的簡潔方法。   首先你要安裝xcode和home brew,xcode直接在應用商店裡面找哈,homebrew安裝如下

Python 3.7.1 基礎 資料型別 列表 元組 字串

序列型別 1.前言 2.序列的通用操作 3. 序列型別 3.1 可變序列型別 3.1.1 列表 List 3.1.1.1 class list([iterable]) 3.1.1.2 s

Python 3.7.1 模組 正則表示式 re

正則表示式操作 1. 正則表示式語法 1.1 特殊字元 . ^ $ * + ? *?,+?,?? {m} {m,n} {m,n}

Python 3.7.1 模組 string

string-常用string操作 1. 字串常量 string.ascii_letters string.ascii_lowercase string.ascii_uppercase string.digits stri