0
class PrepareTableOperator(BaseOperator):
   def _load_table(self):
      drop_table_query: str = ( "drop table if exists " + self.get_table() )

I'm a complete newbie to python but I do have a bit of a Java background. What I don't get is the usage of colons in python. I've googled around, and it's used for slicing and for starting function definitions. But there's no 'def' syntax in the above, so to me this doesn't look like a function.

My question is, what is the colon in Python, is it another assignment operator for dictionary values, similar to a key value pair? Is that what it's doing here? What is it doing here, essentially?

3
  • 1
    yes, it can be used within a dictionary to define a key value pair
    – M Z
    Commented Aug 20, 2020 at 19:56
  • If this is the case, please provide the entire minimally reproducible code, as in the start and end of the dictionary definition (i.e.{, })
    – M Z
    Commented Aug 20, 2020 at 19:57
  • 1
    what is a colon for in python Commented Aug 20, 2020 at 20:00

2 Answers 2

7

In your code snippet, it is a type annotation. It is a relatively new feature of Python that lets you keep track of the data types, so in this case, it is declaring that drop_table_query is a string.

Type annotations are checked by IDE, but not enforced by the Python interpreter. This means that drop_table_query could actually be an int and Python itself won't complain. The type annotation is just a sort of recommendation.

0

What it is doing here is defining that the variable will be a string (str data type). The colon is used in while loops, for loops, if statements, and functions. The colon helps the code flow into the following indented block. A single equal sign is used to assign a value to a variable, a double equal sign is used for conditions, like if var == other_var:. There is also +=, -=, *=, and /=. Those are used to shorten things like, var = var + 1, to var += 1.

Not the answer you're looking for? Browse other questions tagged or ask your own question.