Python - Type Hinting Flashcards

1
Q

Python is a _______ typed language. This means that the Python interpreter does type checking only when ________, and that the type of a variable is _______ over its lifetime.

A

Python is a DYNAMICALLY typed language. This means that the Python interpreter does type checking only when CODE RUNS, and that the type of a variable is ALLOWED TO CHANGE over its lifetime.

Below is an example of variables being allowed to change type:

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

The opposite of dynamic typing is _____ typing. ______ type checks are performed without running the program.

In most ______ typed languages, for instance C and Java, this is done as your program is ______.

A

The opposite of dynamic typing is STATIC typing. STATIC type checks are performed without running the program.

In most STATICALLY typed languages, for instance C and Java, this is done as your program is COMPILED.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

______ typing is a concept related to dynamic typing, where the type or the class of an object is less important than the methods it defines.

Using ______ typing you do not check types at all. Instead you check for the presence of a given method or attribute.

A

DUCK typing is a concept related to dynamic typing, where the type or the class of an object is less important than the methods it defines.

Using DUCK typing you do not check types at all. Instead you check for the presence of a given method or attribute.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How would you add type hinting to the following code? Make text a str, align a bool, and output a str:

A

To add information about types to the function, you simply annotate its arguments and return value as follows.

Adding type hints like this has no runtime effect: they are only hints and are not enforced on their own.

To catch this kind of error you can use a static type checker. That is, a tool that checks the types of your code without actually running it in the traditional sense.

(You might already have such a type checker built into your editor)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a common tool for type checking besides already built in stuff that might be in your code editor?

A

The most common tool for doing type checking is Mypy.

You can run this by typing in your shell:

mypy somecode.py

(spits out something is there is a type error, spits out nothing if not)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How would you type hint a variable?

Type hint the following:

pi = 3.142

A

pi: float = 3.142

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How would you specify typing for the below?

A

You should use the special types defined in the typing module. These types add syntax for specifying the types of elements of composite types. You can write the following:

***Note that each of these types start with a capital letter and that they all use square brackets to define item types

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What type would you use for a function that doesn’t return anything?

What type would you use for a function that returns any sequence?

A

None

Any

ex)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Elementary Type Theory:

One important concept is that of subtypes. Formally, we say that a type T is a subtype of U if the following two conditions hold:

  • Every ____ from T is also in the set of ____of U type.
  • Every ____ from U type is also in the set of ____ of T type.
A

Elementary Type Theory:

One important concept is that of subtypes. Formally, we say that a type T is a subtype of U if the following two conditions hold:

  • Every value from T is also in the set of values of U type.
  • Every function from U type is also in the set of functions of T type.

These two conditions guarantees that even if type T is different from U, variables of type T can always pretend to be U.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Elementary Type Theory:

Since 0 and 1 are both integers, the first condition holds. Above you can see that booleans can be added together, but they can also do anything else integers can. This is the second condition above. In other words, bool is a _____ of int.

The importance of subtypes is that a _____ can always pretend to be its _____.

A

Elementary Type Theory:

Since 0 and 1 are both integers, the first condition holds. Above you can see that booleans can be added together, but they can also do anything else integers can. This is the second condition above. In other words, bool is a subtype of int.

The importance of subtypes is that a subtype can always pretend to be its supertype.

***Subtypes are somewhat related to subclasses. In fact all subclasses corresponds to subtypes, and bool is a subtype of int because bool is a subclass of int. However, there are also subtypes that do not correspond to subclasses. For instance int is a subtype of float, but int is not a subclass of float.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly