Questions tagged «type-hinting»

类型提示将函数参数绑定到特定对象或对其进行强类型化。


3
在Python中使用类型提示添加默认参数值
如果我有这样的功能: def foo(name, opts={}): pass 我想在参数中添加类型提示,该怎么办?我假设的方式给了我一个语法错误: def foo(name: str, opts={}: dict) -> str: pass 以下内容不会引发语法错误,但似乎不是处理这种情况的直观方法: def foo(name: str, opts: dict={}) -> str: pass 我在typing文档或Google搜索中找不到任何内容。 编辑:我不知道默认参数如何在Python中工作,但出于这个问题,我将保留上面的示例。通常,最好执行以下操作: def foo(name: str, opts: dict=None) -> str: if not opts: opts={} pass

9
在PHP 7之前,如何解决“必须是给定字符串的字符串实例”?
这是我的代码: function phpwtf(string $s) { echo "$s\n"; } phpwtf("Type hinting is da bomb"); 导致此错误: 可捕获的致命错误:传递给phpwtf()的参数1必须是字符串的一个实例,字符串给定 看到PHP能够一口气识别并拒绝所需的类型,这不仅仅是一个Orwellian。该死的,有五个灯。 PHP中字符串的类型提示等效于什么?对答案的额外考虑,可以准确解释这里发生的情况。
217 php  types  type-hinting 


4
使用类型提示时无法传递空参数
如下代码: <?php class Type { } function foo(Type $t) { } foo(null); ?> 在运行时失败: PHP Fatal error: Argument 1 passed to foo() must not be null 为什么不允许像其他语言一样传递null?
192 php  type-hinting 


5
我怎样才能告诉PyCharm参数期望是什么类型?
当涉及到构造函数,赋值和方法调用时,PyCharm IDE非常擅长分析我的源代码并弄清楚每个变量应该是什么类型。我很喜欢它,因为它给了我很好的代码完成和参数信息,并且如果我尝试访问一个不存在的属性,它会给我警告。 但是当涉及到参数时,它一无所知。代码完成下拉列表无法显示任何内容,因为它们不知道参数的类型。代码分析无法查找警告。 class Person: def __init__(self, name, age): self.name = name self.age = age peasant = Person("Dennis", 37) # PyCharm knows that the "peasant" variable is of type Person peasant.dig_filth() # shows warning -- Person doesn't have a dig_filth method class King: def repress(self, peasant): # PyCharm has no …

3
PHP7中的可空返回类型
PHP 7引入了返回类型声明。这意味着我现在可以指示返回值是某个类,接口,数组,可调用或新暗示的标量类型之一,对于函数参数而言,这是可能的。 function returnHello(): string { return 'hello'; } 通常,值并不总是存在,并且您可能返回某种类型的值或null。虽然您可以通过将参数的默认值设置为null(DateTime $time = null)来使参数为可空状态,但是似乎没有办法对返回类型执行此操作。确实是这种情况,还是我不知如何找到方法呢?这些不起作用: function returnHello(): string? { return 'hello'; } function returnHello(): string|null { return 'hello'; }

3
* args和** kwargs的类型注释
我正在尝试使用具有抽象基类的Python类型注释来编写一些接口。有没有一种方法来注释可能的类型*args和**kwargs? 例如,如何表达一个函数的明智参数是一个int或两个int?type(args)给出,Tuple所以我的猜测是将类型注释为Union[Tuple[int, int], Tuple[int]],但这是行不通的。 from typing import Union, Tuple def foo(*args: Union[Tuple[int, int], Tuple[int]]): try: i, j = args return i + j except ValueError: assert len(args) == 1 i = args[0] return i # ok print(foo((1,))) print(foo((1, 2))) # mypy does not like this print(foo(1)) print(foo(1, 2)) 来自mypy的错误消息: t.py: …

2
如何在类型提示中指定函数类型?
我想在当前的Python 3.5项目中使用类型提示。我的函数应该接收一个函数作为参数。 如何在类型提示中指定类型函数? 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 我检查了PEP 483,但在那里找不到函数类型提示。

5
类型提示指定类型的列表
使用Python 3的功能注释,可以指定包含在同类列表(或其他集合)中的项的类型,以便在PyCharm和其他IDE中进行类型提示。 一个int列表的伪python代码示例: def my_func(l:list<int>): pass 我知道有可能使用Docstring ... def my_func(l): """ :type l: list[int] """ pass ...但是我更喜欢注释样式。


1
如何注释多个返回值的类型?
我如何使用类型提示来注释一个返回Iterable总是返回两个值的函数:abool和a str?提示Tuple[bool, str]很接近,除了将返回值类型限制为元组,而不是生成器或其他可迭代类型。 我主要是好奇的,因为我想注释一个foo()用于返回多个值的函数,如下所示: always_a_bool, always_a_str = foo() 通常函数喜欢foo()做这样的事情return a, b(它返回一个元组),但我喜欢的类型暗示要足够灵活,以取代发电机或列表或别的东西返回的元组。

5
没有循环导入的Python类型提示
我正试图将我的大班分成两部分;好吧,基本上是进入“主”类和具有其他功能的mixin的,就像这样: main.py 文件: import mymixin.py class Main(object, MyMixin): def func1(self, xxx): ... mymixin.py 文件: class MyMixin(object): def func2(self: Main, xxx): # <--- note the type hint ... 现在,尽管这很好用,但是类型提示MyMixin.func2当然不起作用。我无法导入main.py,因为会进行周期性导入,并且没有提示,我的编辑器(PyCharm)无法分辨出什么self。 我使用的是Python 3.4,如果在那里有解决方案,我愿意移至3.5。 有什么办法可以将我的班级分成两个文件并保留所有“连接”,以便我的IDE仍然可以自动完成以及知道类型的所有其他优点。

2
Python void返回类型注释
在python 3.x中,通常使用函数的返回类型注释,例如: def foo() -> str: return "bar" “ void”类型的正确注释是什么? 我正在考虑3个选择: def foo() -> None: 不是逻辑IMO,因为None不是类型, def foo() -> type(None): 使用我知道的最佳语法NoneType, def foo(): 省略显式的返回类型信息。 选项2对我来说似乎是最合乎逻辑的,但是我已经看到过一些实例1。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.