Fwd: A typing question
Paulo da Silva
p_d_a_s_i_l_v_a_ns at nonetnoaddress.pt
Sun Oct 30 00:48:12 EDT 2022
Às 02:32 de 30/10/22, dn escreveu:
> On 30/10/2022 11.59, Paulo da Silva wrote:
>>> Solution (below) will not work if the mention of Foos in GLOBALS is a
>>> forward-reference. Either move GLOBALS to suit, or surround "Foos"
>>> with quotes.
>> This is the problem for me. So far, without typing, I used to have
>> some config and globals classes, mostly to just group definitions an
>> make the program more readable. A matter of taste and style.
>
> Agreed, a good practice.
Thank you.
>
>
>> Now, "typing" is breaking this, mostly because of this forward
>> reference issue.
>
> As a first step, use the quotation-marks to indicate that such will be
> defined later in the code:-
>
>> class GLOBALS:
>> Foos: Optional[Foos]=None
>
> class GLOBALS:
> Foos: Optional["Foos"]=None
>
>
> Later, as gather (typing) expertise, can become more sophisticated,
> as-and-when...
>
>
>> The funny thing is that if I replace foos by Foos it works because it
>> gets known by the initial initialization :-) !
>
> Is the objective to write (good) code, or merely to satisfy the
> type-checker?
>
> Something that is misleading is not going to be appreciated by others
> (including the +6-months you), eg
>
> a = a + 1 # decrement total
>
> Typing is not compulsory, and has been designed so that we can implement
> it a bit at a time, eg only one function amongst many contained by a
> module - if that's the only code that requires maintenance/update.
>
> Best not to create "technical debt" though!
>
The main idea is to eventually catch some, otherwise "hidden", errors
and produce better and cleaner code. Documentation is also a must.
Regards
More information about the Python-list
mailing list