python-3.5

How can I specify the function type in my type hints?

早过忘川 提交于 2019-11-26 02:37:32
问题 I want to use type hints in my current Python 3.5 project. My function should receive a function as parameter. How can I specify the type function in my type hints? import typing def my_function(name:typing.AnyStr, func: typing.Function) -> None: # However, typing.Function does not exist. # How can I specify the type function for the parameter `func`? # do some processing pass I checked PEP 483, but could not find a function type hint there. 回答1: As @jonrsharpe noted in a comment, this can be

What is the '@=' symbol for in Python?

为君一笑 提交于 2019-11-26 02:32:57
问题 I know @ is for decorators, but what is @= for in Python? Is it just reservation for some future idea? This is just one of my many questions while reading tokenizer.py . 回答1: From the documentation: The @ (at) operator is intended to be used for matrix multiplication. No builtin Python types implement this operator. The @ operator was introduced in Python 3.5. @= is matrix multiplication followed by assignment, as you would expect. They map to __matmul__ , __rmatmul__ or __imatmul__ similar

Type hints: solve circular dependency [duplicate]

这一生的挚爱 提交于 2019-11-26 01:48:25
问题 This question already has an answer here: How do I specify that the return type of a method is the same as the class itself? 4 answers The following produces NameError: name \'Client\' is not defined . How can I solve it? class Server(): def register_client(self, client: Client) pass class Client(): def __init__(self, server: Server): server.register_client(self) 回答1: You can use a forward reference by using a string name for the not-yet-defined Client class: class Server(): def register

“Fire and forget” python async/await

泄露秘密 提交于 2019-11-26 00:36:36
问题 Sometimes there is some non-critical asynchronous operation that needs to happen but I don\'t want to wait for it to complete. In Tornado\'s coroutine implementation you can \"fire & forget\" an asynchronous function by simply ommitting the yield key-word. I\'ve been trying to figure out how to \"fire & forget\" with the new async / await syntax released in Python 3.5. E.g., a simplified code snippet: async def async_foo(): print(\"Do some stuff asynchronously here...\") def bar(): async_foo(

When to use and when not to use Python 3.5 `await` ?

試著忘記壹切 提交于 2019-11-26 00:15:52
问题 I\'m getting the flow of using asyncio in Python 3.5 but I haven\'t seen a description of what things I should be await ing and things I should not be or where it would be neglible. Do I just have to use my best judgement in terms of \"this is an IO operation and thus should be await ed\"? 回答1: By default all your code is synchronous. You can make it asynchronous defining functions with async def and "calling" this functions with await . More correct question is "When should I write

How do I specify that the return type of a method is the same as the class itself?

六眼飞鱼酱① 提交于 2019-11-25 23:59:02
问题 I have the following code in python 3: class Position: def __init__(self, x: int, y: int): self.x = x self.y = y def __add__(self, other: Position) -> Position: return Position(self.x + other.x, self.y + other.y) But my editor (PyCharm) says that the reference Position can not be resolved (in the __add__ method). How should I specify that I expect the return type to be of type Position ? Edit: I think this is actually a PyCharm issue. It actually uses the information in its warnings, and code

What are type hints in Python 3.5?

眉间皱痕 提交于 2019-11-25 23:57:41
问题 One of the most talked about features in Python 3.5 is type hints . An example of type hints is mentioned in this article and this one while also mentioning to use type hints responsibly. Can someone explain more about them and when they should be used and when not? 回答1: I would suggest reading PEP 483 and PEP 484 and watching this presentation by Guido on Type Hinting. In a nutshell : Type hinting is literally what the words mean, you hint the type of the object(s) you're using . Due to the

Type hints: solve circular dependency

穿精又带淫゛_ 提交于 2019-11-25 22:51:51
The following produces NameError: name 'Client' is not defined . How can I solve it? class Server(): def register_client(self, client: Client) pass class Client(): def __init__(self, server: Server): server.register_client(self) You can use a forward reference by using a string name for the not-yet-defined Client class: class Server(): def register_client(self, client: 'Client') pass As of Python 3.7 , you can also postpone all runtime parsing of annotations by adding the following __future__ import at the top of your module: from __future__ import annotations at which point the annotations