Specify your own class in class argument and return type annotation in Python

Cannot specify own class in type annotation

list_node.py


class ListNode:
    def __init__(self, val: int, next: ListNode):
        self.val = val
        self.next = next

When I wrote a type annotation like the above (one-way linked list constructor), a warning was displayed if ListNode was not defined.

undefined name 'ListNode'

For Python 3.7 and above: Use from __future__ import annotations

list_node.py


from __future__ import annotations


class ListNode:
    def __init__(self, val: int, next: ListNode):
        self.val = val
        self.next = next

assert ListNode.add.__annotations__ == {
    'next': 'ListNode'
}

For Python 3.7 and below: Use strings

Since from __future__ import annotations cannot be used in Python 3.7 or less, use a string.

list_node.py


class ListNode:
    def __init__(self, val: int, next: 'ListNode'):
        self.val = val
        self.next = next

reference

Recommended Posts

Specify your own class in class argument and return type annotation in Python
Create your own graph structure class and its drawing in python
[Road to intermediate Python] Define in in your own class
Function argument type definition in python
[Python] logging in your own module
Check the argument type annotation when executing a function in Python and make an error
Create your own Linux commands in Python
[LLDB] Create your own command in Python
Easily use your own functions in Python
[Python] Package and distribute your own modules
Get your own IP address in Python
[Python] Implement your own list-like class using collections.UserList
Argument implementation (with code) in your own language
[python] Difference between variables and self. Variables in class
I wrote a class in Python3 and Java
Import your own modules in Grasshopper's Python development
python: Use your own class for numpy ndarray
[Python] Class type and usage of datetime module
Create your own Big Data in Python for validation
[Python of Hikari-] Chapter 06-02 Function (argument and return value 1)
Specify a subcommand as a command line argument in Python
Use the CASA Toolkit in your own Python environment
Convenient argument passing technique in Python (tuple, dictionary type)
Get your current location and user agent in Python
Difference between return, return None, and no return description in Python
Class notation in Python
Sample of getting module name and class name in Python
[Python] When you want to import and use your own package in the upper directory
List method argument information for classes and modules in Python
Try sorting your own objects with priority queue in Python
Get the MIME type in Python and determine the file format
How to use pyenv and pyenv-virtualenv in your own way
Reference order of class variables and instance variables in "self. Class variables" in Python
I created a class in Python and tried duck typing
Python: Class and instance variables
Python class variables and instance variables
Stack and Queue in Python
Unittest and CI in Python
I was addicted to confusing class variables and instance variables in Python
[Python] Make your own LINE bot
MIDI packages in Python midi and pretty_midi
Difference between list () and [] in Python
perl objects and python class part 2.
View photos in Python and html
Sorting algorithm and implementation in Python
Landmines hidden in Python class variables
Python, yield, return, and sometimes yield from
Python class definitions and instance handling
Manipulate files and folders in Python
About dtypes in Python and Cython
[python] week1-3: Number type and operation
Dynamically load json type in python
Read PNG chunks in Python (class)
Assignments and changes in Python objects
Type specified in python. Throw exceptions
Check and move directories in Python
Ciphertext in Python: IND-CCA2 and RSA-OAEP
Hashing data in R and Python
Function synthesis and application in Python
Examine the object's class in python
perl objects and python class part 1.