There is how we define arrays in python:
a = []
and it can contains any elements of any types. here is my question:
I have a class:
class Foo:
def __init(self,length:int):
self.__length = length
@property
def Length(self) -> int
return self.__length
in my main I want to define an array of type Foo so I can have access to it's properties, method , etc using intelliscense of an IDE like pyCharm.
How can I declare an array like this?
foos: Foo[]
-
Not sure this is possible - per this questionCDJB– CDJB2020年01月29日 16:47:59 +00:00Commented Jan 29, 2020 at 16:47
-
I’m not sure I understand your question, can you explain things a bit more? It isn’t clear if Foo is meant to be the sequence or the element. There is how we define arrays in python: No, that’s a list.AMC– AMC2020年01月29日 18:11:48 +00:00Commented Jan 29, 2020 at 18:11
1 Answer 1
With pure "basic" Python your cannot (AFAIK)
That said, you could be greatly interested in using the typing package which provides ways to define custom types that are recognised and handled by pyCharm autocompletion mechanism when used with type hints.
From your example, you can do this:
from typing import List
from somewhere import Foo
FooArray = List[Foo]
def bar(foos: FooArray) -> None:
for foo in foos:
print(foo.Length) # pyCharm suggests the `Length` property