[Tutor] problem with creating paths

Shall, Sydney sydney.shall at kcl.ac.uk
Wed Oct 17 13:21:18 EDT 2018

On 17/10/2018 18:18, Mats Wichmann wrote:
> On 10/17/2018 10:07 AM, Shall, Sydney via Tutor wrote:
>> Firstly, I would like to thank Steven for reminding me of the assert
>> statement. I should have remembered this. It allowed me to isolate the
>> problem, which predictably (for me) was very elementary. I am too
>> embarrassed to say how simple the error was.
>> However, my original problem was not solved by correcting this error.
>> So, I will now try and narrow down the location of the problem and then
>> if I cannot solve it, I shall return for more good advice.
>> Many thanks to Steven and to Peter.
> I'll weigh in with a mini- (and unasked-for-) lecture here:
> this is often the point at which someone says "boy, I wish Python were
> strongly typed, so things didn't change types in flight".
> But in fact, the list didn't change types, it's still a list. In fact we
> even know where that list is: it's the first element of that tuple you
> ended up with. The _name_ you gave to that list carries no typing
> meaning, however (although you can give it type hints that an external
> tool could use to warn you that you are changing something).  So you
> have somewhere given that name to a completely different object, a tuple
> which contains your list and another element. So clearly what you're
> looking for is the place that happens.
> So here's a sketch of how you might use type hinting to find this, to
> bring it back to something practical:
> === types.py:
> from typing import List, Tuple
> a: List[str] = [
>      '/a/path',
>      '/b/path',
> ]
> print(type(a))
> print(a)
> a = (a, '/c/path')
> print(type(a))
> print(a)
> === this works just fine:
> $ python3 types.py
> <class 'list'>
> ['/a/path', '/b/path']
> <class 'tuple'>
> (['/a/path', '/b/path'], '/c/path')
> === but a hinting tool can see a possible issue:
> $ mypy types.py
> types.py:10: error: Incompatible types in assignment (expression has
> type "Tuple[List[str], str]", variable has type "List[str]")

Thanks for this. Most helpful.
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://emea01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fmail.python.org%2Fmailman%2Flistinfo%2Ftutor&data=01%7C01%7Csydney.shall%40kcl.ac.uk%7Ca58896d72c374cda3c1508d63454b783%7C8370cf1416f34c16b83c724071654356%7C0&sdata=OHZtedYdy0UHKDagLO1TI%2BUIjEJuzRjZjD4HRdSQmNI%3D&reserved=0



Professor Sydney Shall
Department of Haematology/Oncology
Phone: +(0)2078489200
E-Mail: sydney.shall
[Correspondents outside the College should add @kcl.ac.uk]

More information about the Tutor mailing list