Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

How to type hint a "float-like" object?#2038

Discussion options

How do I make type hint for a function, which takes any kind for object that behaves like a float?
For example, consider

defcubed(x):returnx**3

This function can accept a float (f(5)), but also any other kind of object which supports the same operations as float:

pandas series:

s=pd.Series([1,2,3])cubed(s)

xarray dataarray

a=xr.DataArray([1,2,3],dims='x')cubed(a)
You must be logged in to vote

You might want to use a protocol with a__pow__ method (since the** operator calls__pow__). Search fortyping.Protocol for more. There are many things "float-like" could mean but a Protocol should generally be able to express them.

Replies: 5 comments

Comment options

Depending on how precise you want to be, something like numpy'sArrayLike or a customSupportsPow protocol (example) may be what you're looking for.

In the future, questions like this should generally be asked in thediscussions tab or athttps://discuss.python.org/c/help/7

You must be logged in to vote
0 replies
Comment options

Hmm, but ArrayLike is kind of opposite, right? This is for objects that have array-like behaviour (which includes a scalar instance of a float). What I am looking at, are objects that have a float-like behaviour (which includes arrays of floats). Does that make sense? I was wondering if the was some kind of FloatLike (or maybe more mathematically correct: a RealNumberLike) protocol intyping, but I can't really seem to find something like this.

Sorry for asking in the wrong place; I was too hastly there. I can move the discussion to there, if you like :-)

You must be logged in to vote
0 replies
Comment options

You might want to use a protocol with a__pow__ method (since the** operator calls__pow__). Search fortyping.Protocol for more. There are many things "float-like" could mean but a Protocol should generally be able to express them.

You must be logged in to vote
0 replies
Answer selected bysrittau
Comment options

Hmm, I would suggest that "float-like" means it supports all the methods of the builtin float object definedhere.

So I guess a Protocol with these method would suffice maybe? Could this be part oftyping?

You must be logged in to vote
0 replies
Comment options

So I guess a Protocol with these method would suffice maybe?

Whether it suffices or not really depends on your code. I would probably find it surprising if the thing you're thinking of as float-like actually returns float for some of those methods. Try it and see, your type checker should tell if you if your Protocol has things your type doesn't satisfy or if you use things not described by your Protocol!

Could this be part of typing?

There's a high bar for adding things to the standard library, particularly when they can easily be defined outside the standard library and haven't yet been evidenced to be highly desired.

You must be logged in to vote
0 replies
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Category
Q&A
Labels
topic: otherOther topics not covered
4 participants
@UlrikChristensenGit@JelleZijlstra@hauntsaninja@brianschubert
Converted from issue

This discussion was converted from issue #2037 on July 02, 2025 10:55.


[8]ページ先頭

©2009-2025 Movatter.jp