Videos
type A = string creates an alias for string. Whenever you use A in your code, it works just like string. So for example, you can't define methods on it.
type A string defines a new type, which has the same representation as string. You can convert between an A and string at zero cost (because they are the same), but you can define methods on your new type, and reflection will know about the type A.
For example (on the playground)
package main
import (
"fmt"
)
type A = string
type B string
func main() {
var a A = "hello"
var b B = "hello"
fmt.Printf("a is %T\nb is %T\n", a, b)
}
Output:
a is string
b is main.B
To be clear on terms, these both are type declarations.
type A = string
This is called alias declaration. I.e. you creating an alias to a type. Basically, there's no difference between type and alias. You can define methods on alias and they will be available for initial type instances. Example:
type A struct {}
type B = A
func (B) print() {
fmt.Println("B")
}
func main() {
a := A{}
b := B{}
a.print() // OUTPUT: B
b.print() // OUTPUT: B
}
Although, in your particular example type A = string you can't define methods on it, because string is non-local type(there was a proposal to add ability to create methods for non-local types but it got rejected).
Your second case type A string is type definition. Basically it's creating new type that has all fields of original type, but not it's methods. Example:
type A struct {}
func (A) print() {
fmt.Println("A")
}
type B A
func (B) print() {
fmt.Println("B")
}
func main() {
a := A{}
b := B{}
a.print() // OUTPUT: A
b.print() // OUTPUT: B
}
Python 3.5+
Since Python 3.5 you may use typing module.
Quoting docs, A type alias is defined by assigning the type to the alias:
Copy# Python 3.5-3.8
from typing import List
Vector = List[float]
# Python 3.9+
Vector = list[float] # No import needed, lower case l
To learn more about enforcing types in Python you may want to get familiar with PEPs: PEP483 and PEP484.
Python historically was using duck-typing instead of strong typing and hadn't built-in way of declaring types before 3.5 release.
Python 3.10+
Since Python 3.10, the TypeAlias annotation is available in the typing module.
It is used to explicitly indicate that the assignment is done to generate a type alias. For example:
CopyPoint: TypeAlias = tuple[float, float]
Triangle: TypeAlias = tuple[Point, Point, Point]
You can read more about the TypeAlias annotation on the PEP 613 that introduced it.
Hi
Im reading through the typing documentation https://docs.python.org/3/library/typing.html and have a question that I cannot answer.
In the past when I wanted to use type aliases I would use code like Vector = list[float] (I think that I must have picked this up from a post on stack overflow or something).
However, in the document above it suggests using the code type Vector = list[float].
The difference between the two is that the data type is types.GenericAlias (the list[float]) for the first Vector and typing.TypeAliasType for the second Vector.
But besides that I am not really sure what is the difference between these two methods. Im not sure where the reason to use one over the other is. Im also not sure where the documntation is for the first example (maybe technically this is not a Type Alias).
Im not sure if anyone can help here?