[Tutor] the use of the COLON

Alan Gauld alan.gauld at yahoo.co.uk
Thu May 11 19:58:01 EDT 2023


On 12/05/2023 00:08, ThreeBlindQuarks via Tutor wrote:
> When you say something WORKS, what do you mean?

In this case it means the variable dummy_user_list is assigned the value
in the statement, ie [[...]]

> Do you mean later code indicates it did what was expected?

No, the later code ignores the type definition completely, it's
intended for third party tools to pick up on - I guess linters etc?

> This may sound like a dumb question but there seem to be a few anomalies in the code:
> 
> dummy_user_list: list=[["johnf", "jfabiani1947"]]
> 
> The first is this:
> 
> dummy_user_list:
> 
> Python does have a concept of starting an indented scope with a colon 

This is not an indented code type colon but a type definition.
I admit I'm not a fan of the type hinting stuff in Python but
it is what it is, and this is it...

So the colon in this case is saying that the thing that follows
is a type (and optional assignment)

> On my machine, code like:
> 
> hello:
> 
> gets an error method about syntax.

Yes because ther is no type provided

hello: str

would work and tell Python/linter that the variable hello should
be a string.

> Second, the word "list" is the name of a function and at best

Technically its the name of a type and the type constructor.
Which is treated a little differently from regular functions.

> it can be very poor form to use it as a variable name. 

Despite being on the left of an assignment it is actually being used as
a type name in this statement. The variable name is dummy_user_list


> In the code you shared that does not generate an error for me 
> either, it is NOT setting a variable called list to a value:

Thats true it is setting the variable dummy_user_list to the
value in the assignment which corresponds with the type hint,
namely a list. (a list of lists in this case but Python doesn't
care, it's still a list!)

>>>> dummy_user_list: list=[["johnf", "jfabiani1947"]]
>>>> list
> <class 'list'>
>>>> print(list)
> <class 'list'>

Try

>>>> dummy_user_list
> [['johnf', 'jfabiani1947']]

> But if I leave out the meaningles colon part, it does overwrite:

Not meaningless, it is a type declaration.

>>>> list=[["johnf", "jfabiani1947"]]

But this is just a regular assignment so you override the name list.

> Finally, what was the purpose of [["johnf", "jfabiani1947"]] 
> versus just ["johnf", "jfabiani1947"] in Python? 

Its a list of lists. The first element is ["johnf", "jfabiani1947"]
but others could be added:

>>> dummy_user_list.append(['another', 'value'])
>>> dummy_user_list
[['johnf', 'jfabiani1947'], ['another', 'value']]


> Back to your question, on MY SETUP, the code you mentioned only 
> works in the sense of no error but does nothing useful. 

That's correct for vanilla Python. I believe there are tools that
will spit out type warnings etc based on earlier hints.

> Anything after the : may as as well be a comment but not exactly.

Not exactly, the assignment part of the statement is still valid
and does assign a value to the variable.

> But it gets weirder when I wonder if "hello" is seen as a variable. 
> I chose new undefined names:
> 
>>>> print(myname)
> NameError: name 'myname' is not defined
>>>> who="me"
>>>> myname: who="Quark"

Because vanilla Python doesn't do anything with the type declaration it
accepts any recognised name as a type. An unrecognised name gives an error:

>>> y:blippidy=6
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'blippidy' is not defined
>>> y:dummy_user_list=666
>>> y
666

So although 666 is not a list(the type of dummy_user_list) Python
silently accepts the name then assigns an integer to the variable!

>>>> print(who)
> me
>>>> print(myname)
> Quark

So the assignment to myname works as expected without any error.

> So the above shows that at the start, there was no variable 
> called "myname" and there was one of 'who' and yet the assignment 
> to who was NOT done and yet passed along to 'myname'!!!

Nope, 'who' was recognised as a potential type name and the assignment
was made directly to myname.

> You could experiment more, but this behavior does not look 
> like it was planned to be used by us!

It was planned to be read by tools and written by programmers
who get very uptight about static typing!

>>>> mytown: z="three"
>>>>   x="one"
>>>>   y="two"
>>>>

I get an indentation error when I try that...

> But another egg sperm meant indicates the scope is LOCAL

Nope, nothing to do with scopes here. Just a slightly
confusing type declaration syntax. One which does nothing
much of any use in the real world! (But might do someday...)

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos





More information about the Tutor mailing list