You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 in typing, 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 might want to use a protocol with a __pow__ method (since the ** operator calls __pow__). Search for typing.Protocol for more. There are many things "float-like" could mean but a Protocol should generally be able to express them.
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.
Activity
brianschubert commentedon Jul 1, 2025
Depending on how precise you want to be, something like numpy's
ArrayLikeor a customSupportsPowprotocol (example) may be what you're looking for.In the future, questions like this should generally be asked in the discussions tab or at https://discuss.python.org/c/help/7
UlrikChristensenGit commentedon Jul 1, 2025
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 in
typing, 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 :-)
JelleZijlstra commentedon Jul 1, 2025
You might want to use a protocol with a
__pow__method (since the**operator calls__pow__). Search fortyping.Protocolfor more. There are many things "float-like" could mean but a Protocol should generally be able to express them.UlrikChristensenGit commentedon Jul 2, 2025
Hmm, I would suggest that "float-like" means it supports all the methods of the builtin float object defined here.
So I guess a Protocol with these method would suffice maybe? Could this be part of
typing?hauntsaninja commentedon Jul 2, 2025
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!
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.