Re: [Tutor] What is Tuple in the typing module?

2019-08-20 Thread C W
Thank you, Peter and Alan. Both very helpful. I was able to figure it out.
Cheers!

On Sat, Aug 17, 2019 at 5:45 AM Alan Gauld via Tutor 
wrote:

> On 17/08/2019 00:46, C W wrote:
>
> The formatting seems messed up I'll try to straighten it out.
> I hope I get it right!
>
> Caveat: I'm no expert in the typing module, I've read the
> docs but never found any use for it.
>
> > What exactly is Tuple in the typing module? What does it do?
>
> It specifies a tuple type. I assume you understand what
> a tuple is in regular Python code?
>
> > This is the definition from its website.
> > https://docs.python.org/3/library/typing.html
> > "A type alias is defined by assigning the type to the alias"
> >
> > I have no idea what that means.
>
> It just means that an alias is a label that you can use
> to signify a type. And you can effectively create new
> "types" by combining simpler types
>
>
> > Here's the example from the documentation:
> >
> > from typing import Dict, Tuple, Sequence
> ConnectionOptions = Dict[str, str]
> Address = Tuple[str,int]Server  = Tuple[Address, ConnectionOptions]
>
> So this creates 3 new type aliases: ConnectionOptions, Address, Server
> which are defined in terms of basic Python types,.
> So Address is a tuple of a string and integer.
>
> > def broadcast_message(message: str, servers: Sequence[Server]) -> None:
>
> Ad this defines a function where the parameter types are
> specified in terms of the new type names that we just
> created.
>
> > # The static type checker will treat the previous type signature as#
> > being exactly equivalent to this one.
>
> > def broadcast_message(
> > message: str,
> > servers: Sequence[Tuple[Tuple[str, int], Dict[str, str]]]) ->
> None:
>
> And this is the same function but with the parameters expanded
> into their traditional basic types.
>
> Without all the type hinting gubbins it would simply be:
>
> def broadcast_message(message, servers):
>  
>
> So all the detritus above is doing is attempting to make it
> clearer what the two parameters actually are in data terms.
> The first is a string, the second is a complex structure
> of nested tuples and a dict (which probably should all
> be wrapped in a class!).
>
> > I think this even more confusing. Can someone explain this in simple
> words?
> > I don't have a intense computer science background.
>
> Type hints are a way to specify the types of function parameters.
> They are an attempt to make complex type structures more readable
> rather than fixing the data to be simpler (which, to be fair,
> may not always be possible/easy). They are also an attempt to
> bring some of the "benefits" of static typing into Python but
> with very limited success. But they are only hints, you can
> happily survive without them.
>
> --
> 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
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What is Tuple in the typing module?

2019-08-17 Thread Alan Gauld via Tutor
On 17/08/2019 00:46, C W wrote:

The formatting seems messed up I'll try to straighten it out.
I hope I get it right!

Caveat: I'm no expert in the typing module, I've read the
docs but never found any use for it.

> What exactly is Tuple in the typing module? What does it do?

It specifies a tuple type. I assume you understand what
a tuple is in regular Python code?

> This is the definition from its website.
> https://docs.python.org/3/library/typing.html
> "A type alias is defined by assigning the type to the alias"
> 
> I have no idea what that means.

It just means that an alias is a label that you can use
to signify a type. And you can effectively create new
"types" by combining simpler types


> Here's the example from the documentation:
> 
> from typing import Dict, Tuple, Sequence
ConnectionOptions = Dict[str, str]
Address = Tuple[str,int]Server  = Tuple[Address, ConnectionOptions]

So this creates 3 new type aliases: ConnectionOptions, Address, Server
which are defined in terms of basic Python types,.
So Address is a tuple of a string and integer.

> def broadcast_message(message: str, servers: Sequence[Server]) -> None:

Ad this defines a function where the parameter types are
specified in terms of the new type names that we just
created.

> # The static type checker will treat the previous type signature as#
> being exactly equivalent to this one.

> def broadcast_message(
> message: str,
> servers: Sequence[Tuple[Tuple[str, int], Dict[str, str]]]) -> None:

And this is the same function but with the parameters expanded
into their traditional basic types.

Without all the type hinting gubbins it would simply be:

def broadcast_message(message, servers):
 

So all the detritus above is doing is attempting to make it
clearer what the two parameters actually are in data terms.
The first is a string, the second is a complex structure
of nested tuples and a dict (which probably should all
be wrapped in a class!).

> I think this even more confusing. Can someone explain this in simple words?
> I don't have a intense computer science background.

Type hints are a way to specify the types of function parameters.
They are an attempt to make complex type structures more readable
rather than fixing the data to be simpler (which, to be fair,
may not always be possible/easy). They are also an attempt to
bring some of the "benefits" of static typing into Python but
with very limited success. But they are only hints, you can
happily survive without them.

-- 
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


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What is Tuple in the typing module?

2019-08-17 Thread Peter Otten
C W wrote:

> Hi everyone,
> 
> What exactly is Tuple in the typing module? What does it do?
> 
> This is the definition from its website.
> https://docs.python.org/3/library/typing.html
> "A type alias is defined by assigning the type to the alias"
> 
> I have no idea what that means.
> 
> Here's the example from the documentation:
> 
> from typing import Dict, Tuple, Sequence
> ConnectionOptions = Dict[str, str]Address = Tuple[str, int]Server =
> Tuple[Address, ConnectionOptions]
> def broadcast_message(message: str, servers: Sequence[Server]) -> None:
> ...
> # The static type checker will treat the previous type signature as#
> being exactly equivalent to this one.def broadcast_message(
> message: str,
> servers: Sequence[Tuple[Tuple[str, int], Dict[str, str]]]) ->
> None:
> ...
> 
> 
> I think this even more confusing. Can someone explain this in simple
> words? I don't have a intense computer science background.

After the line

ANSWER = 42

you can write

print("The answer is", ANSWER)

rather than

print("The answer is", 42)

Likewise, after

Location = Tuple[float, float, float]

"Location" has become an "alias" for a tuple of 3 floats, and you can write

def distance(a: Location, b: Location) --> float: ...

instead of

def distance(
a: Tuple[float, float, float],
b: Tuple[float, float, float]
) --> float: ...

Basically, use descriptive names for types rather than repeating their 
definition.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] What is Tuple in the typing module?

2019-08-17 Thread C W
Hi everyone,

What exactly is Tuple in the typing module? What does it do?

This is the definition from its website.
https://docs.python.org/3/library/typing.html
"A type alias is defined by assigning the type to the alias"

I have no idea what that means.

Here's the example from the documentation:

from typing import Dict, Tuple, Sequence
ConnectionOptions = Dict[str, str]Address = Tuple[str, int]Server =
Tuple[Address, ConnectionOptions]
def broadcast_message(message: str, servers: Sequence[Server]) -> None:
...
# The static type checker will treat the previous type signature as#
being exactly equivalent to this one.def broadcast_message(
message: str,
servers: Sequence[Tuple[Tuple[str, int], Dict[str, str]]]) -> None:
...


I think this even more confusing. Can someone explain this in simple words?
I don't have a intense computer science background.

Thanks a lot!
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor