Arsalan
Arsalan

Reputation: 744

Array of certain types in Python

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[]

Upvotes: 2

Views: 475

Answers (1)

Tryph
Tryph

Reputation: 6209

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

Upvotes: 2

Related Questions