current position:Home>Can you add a syntax sugar to Cpython to check the type?

Can you add a syntax sugar to Cpython to check the type?

2022-07-25 16:59:21segmentfault

python Yes typing hint !

def upload(content: str) -> bool:
    pass

however content Can pass any type , such as str、bytes、int、float wait

therefore , I will do this :

def upload(content: str) -> bool:
    assert isinstance(content, str) # 
    pass

It can solve the problem of type verification ! But this is very inelegant ! Because every function has to add assert, The code will become very ugly and redundant !

So I want cpython Add a grammar sugar

def upload(content:: str) -> bool:
    pass

hold typing hint Single colon of : Change to double colon ::

Double colon :: It means automatically according to the following typing hint Do type verification !

A single colon indicates pure typing hint
Double colons indicate typing hint + check type

I think this is a great grammar candy , What should I do to make cpython Those people who 100% Realize this new function ?

Give Way python Great again


Other answers 1:

copyright notice
author[segmentfault],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/206/202207211350584875.html

Random recommended