typing: property/setter and lists?

dn PythonList at DancesWithMice.info
Thu Nov 3 03:55:43 EDT 2022


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 "_"?
-- 
Regards,
=dn


More information about the Python-list mailing list