typing: property/setter and lists?
Paulo da Silva
p_d_a_s_i_l_v_a_ns at nonetnoaddress.pt
Thu Nov 3 14:42:35 EDT 2022
Às 07:55 de 03/11/22, dn escreveu:
> On 03/11/2022 16.24, Paulo da Silva wrote:
>> class C:
>> def __init__(self):
>> self.__foos=5*[0]
>>
>> @property
>> def foos(self) -> list[int]:
>> return self.__foos
>>
>> @foos.setter
>> def foos(self,v: int):
>> self.__foos=[v for __i in self.__foos]
>>
>> c=C()
>> c.foos=5
>> print(c.foos)
>> _______________________________________
>>
>> mypy gives the following error:
>> error: Incompatible types in assignment (expression has type "int",
>> variable has type "List[int]")
>
>
> To help us to help you please copy-paste the *exact* message -
> especially which line is in-question.
>
>
> The above code passes without complaint in PyCharm, and executes.
>
>
> However, the general rule?convention would be to establish type at the
> first mention of the identifier, eg
>
> def __init__(self):
> self.__foos:list[int] = 5 * [ 0 ]
> # or [ 0, 0, 0, 0, 0, ]
>
>
> Why the "__i", and not "i", or "_"?
Just because of my personal taste.
I know that __ means (for me) a "not used anymore" var and i is an
indexer/counter/...
Regards.
Paulo
More information about the Python-list
mailing list