Fwd: A typing question
dn
PythonList at DancesWithMice.info
Sat Oct 29 22:32:45 EDT 2022
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.
> 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!
--
Regards,
=dn
More information about the Python-list
mailing list