Type declaration for complex data structures in python

I am new to python programming (background C / C ++). I write code where I need to use complex data structures, such as list dictionary dictionaries. The problem is that when I have to use these objects, I barely remember their structure and therefore, how to access them. This makes it difficult to resume work on code that has not been affected for several days. A very bad solution is to use comments for each variable, but it is very inflexible. So, given that python variables are just pointers to memory, and they cannot be statically declared, is there any convention or rule that I could use to simplify the use of complex data structures?

+3
source share
4 answers

If you use docstrings in your classes, you can use help(vargoeshere)to find out how to use it.

+3
source

Whatever you do, DO NOT repeat, DO NOT use Hungarian notation! This causes severe head and bit rot.

So what can you do? Python and C / C ++ are completely different. In C ++, you usually handle polymorphic calls, for example:

void doWithFooThing(FooThing *foo) {
    foo->bar();
}

Dynamic polymorphism in C ++ depends on inheritance: a pointer passed to doWithFooThing can only point to instances of FooThing or one of its subclasses. Not so in Python:

def do_with_fooish(fooish):
    fooish.bar()

(.. , barableableable bar), , ​​ .

, ++, , (base-) , , Python , . , Python, - , , . . :

def some_action(a_list):
    ...

def some_action(seq):
    ...

seq , , , , dict, set, iterator, .

, , . :

dict_of_strings_to_dates = {}

:

users_birthdays = {}

, , C/++. , .

: Python . , dicionary:

assert foo.bar == getattr(foo, 'bar') == foo.__dict__['bar']

, , docs.python.org.

, BTW, Python , , C/++. .

+2

, , , ... Pythonic? . , , C/++.

+1

- .

0

Source: https://habr.com/ru/post/1778143/


All Articles