2

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[]
2
  • Not sure this is possible - per this question Commented 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. Commented Jan 29, 2020 at 18:11

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.