python限定方法参数类型、返回值类型、变量类型等
<h2 id="typing模块的作用">typing模块的作用</h2><p>自python3.5开始,PEP484为python引入了类型注解(type hints)</p>
<ul>
<li>类型检查,防止运行时出现参数和返回值类型、变量类型不符合。</li>
<li>作为开发文档附加说明,方便使用者调用时传入和返回参数类型。</li>
<li>该模块加入后并不会影响程序的运行,不会报正式的错误,只有提醒pycharm目前支持typing检查,参数类型错误会黄色提示</li>
</ul>
<p><strong>官网typing详细说明</strong><br>
typing类型标注</p>
<h2 id="常用类型">常用类型</h2>
<ul>
<li>
<p>int,long,float: 整型,长整形,浮点型</p>
</li>
<li>
<p>bool,str: 布尔型,字符串类型</p>
</li>
<li>
<p>List, Tuple, Dict, Set:列表,元组,字典, 集合</p>
</li>
<li>
<p>Iterable,Iterator:可迭代类型,迭代器类型</p>
</li>
<li>
<p>Generator:生成器类型</p>
</li>
</ul>
<h2 id="基本类型指定">基本类型指定</h2>
<ul>
<li>示例</li>
</ul>
<pre><code class="language-python">def test(a:int, b:str) -> str:
print(a, b)
return 1000
if __name__ == '__main__':
test('test', 'abc')
</code></pre>
<pre><code>函数test,
a:int指定了输入参数a为int类型,
b:strb为str类型,
-> str返回值为srt类型。
可以看到,
在方法中,我们最终返回了一个int,此时pycharm就会有警告;
当我们在调用这个方法时,参数a我们输入的是字符串,此时也会有警告;
但非常重要的一点是,pycharm只是提出了警告,但实际上运行是不会报错,毕竟python的本质还是动态语言
</code></pre>
<h2 id="复杂的类型标注">复杂的类型标注</h2>
<ul>
<li>示例1</li>
</ul>
<pre><code class="language-python">from typing import List
Vector = List
def scale(scalar: float, vector: Vector) -> Vector:
return
# typechecks; a list of floats qualifies as a Vector.
new_vector = scale(2.0, )
</code></pre>
<ul>
<li>示例2</li>
</ul>
<pre><code class="language-python">from typing import Dict, Tuple, Sequence
ConnectionOptions = Dict
Address = Tuple
Server = Tuple
def broadcast_message(message: str, servers: Sequence) -> None:
...
# The static type checker will treat the previous type signature as
# being exactly equivalent to this one.
def broadcast_message(
message: str,
servers: Sequence, Dict]]) -> None:
...
):
...
</code></pre>
<pre><code>这里需要注意,元组这个类型是比较特殊的,因为它是不可变的。
所以,当我们指定Tuple时,就只能传入长度为2,
并且元组中的所有元素都是str类型
</code></pre>
<h2 id="泛型指定">泛型指定</h2>
<ul>
<li>示例</li>
</ul>
<pre><code class="language-python">from typing import Sequence, TypeVar, Union
T = TypeVar('T') # Declare type variable
def first(l: Sequence) -> T: # Generic function
return l
T = TypeVar('T')# Can be anything
A = TypeVar('A', str, bytes)# Must be str or bytes
A = Union # Must be str or None
</code></pre>
<h2 id="创建变量时的类型指定">创建变量时的类型指定</h2>
<pre><code class="language-python">from typing import NamedTuple
class Employee(NamedTuple):
name: str
id: int = 3
employee = Employee('Guido')
assert employee.id == 3
</code></pre>
<h2 id="不足之处">不足之处</h2>
<ul>
<li>示例</li>
</ul>
<pre><code class="language-python">from typing import List
def test(b: List) -> str:
print(b)
return 'test'
if __name__ == '__main__':
test()
</code></pre>
<pre><code>从这个例子可以看出来,虽然我们指定了List即由int组成的列表,
但是,实际中,只要这个列表中存在int(其他的可以为任何类型),就不会出现警告
</code></pre><br><br>
来源:https://www.cnblogs.com/linkenpark/p/11676297.html
頁:
[1]