Peter Lucas
Peter Lucas

Reputation: 1991

Python variable assignment with : when building SQL query

I've inherited some code and was curious as why it is being used and how it works. The code is as follows:

strsql: str = sql.SWAP_SQL.format(*sql_clauses)

No issue with the right hand side of the assignment, it's simply passing paramaters to a sql string. I'm curious as to the strSQL: str = portion. What exactly is this doing as sql.SWAP_SQL.format(*sql_clauses) and strsql appear to have the exact same sql string?

Upvotes: 0

Views: 79

Answers (2)

Brendan Martin
Brendan Martin

Reputation: 639

That is a type hint. It's hinting that the type of strsql is a str.

Upvotes: 0

nickyfot
nickyfot

Reputation: 2019

Python 3 (i think .5 but maybe earlier) introduced type hints for methods and variables. The hints do not enforce the types (you can but not by default), they are there mostly for readability (basically just hinting the proper format).

Pep-484 has some more details on method/class type hints, Pep-526 has syntax guidelines for variable type hints (your example), and you can also check typing library documentation.

Upvotes: 1

Related Questions