简体   繁体   中英

What is the following code of Python doing?

I was going through a in-built python module and trying to understand it. There are many places where I got stuck. Below is a code line which I found which sums up all of my doubts.

I would like to know what the following code is doing.

def count(self, x: str, __start: SupportsIndex | None = ..., __end: SupportsIndex | None = ...) -> int: ...

This is a type stub, not the actual implementation code. It's just saying that there is a method called count (which is implemented in some other file) and it takes the arguments:

  • x (required argument, is a str )
  • __start (optional argument, can be a SupportsIndex or None )
  • __end (optional argument, can be a SupportsIndex or None )

The self parameter indicates that it's an instance method (for whatever class you found this line of code in). count returns an int value, presumably the number of whatever got counted.

SupportsIndex isn't a builtin Python type, and is presumably implemented in this same module.

Type stubs like this (usually in .pyi files) are common for modules that were either originally written without type annotations or are compiled. The type stub allows code that imports the module to get the benefit of type annotations (ie being able to type-check usages of the imported module) even though the underlying implementation doesn't have Python type annotations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM