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 <tutor@python.org> 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