pitrou commented on code in PR #45818:
URL: https://github.com/apache/arrow/pull/45818#discussion_r2101915118


##########
python/pyarrow/scalar.pxi:
##########
@@ -238,6 +241,9 @@ cdef class UInt8Scalar(Scalar):
         cdef CUInt8Scalar* sp = <CUInt8Scalar*> self.wrapped.get()
         return sp.value if sp.is_valid else None
 
+    def __int__(self):
+        return self.as_py()

Review Comment:
   Sorry for noticing this only now, but `__index__` would actually be better 
as it supports more situations than `__int__` (`__index__` is for objects that 
convert _losslessly_ to an integer, which is the case here, while `__int__` can 
be defined on e.g. floats):
   ```python
   >>> class Int:
   ...:     def __int__(self): return 42
   ...: 
   >>> class Index:
   ...:     def __index__(self): return 42
   ...: 
   >>> int(Int())
   42
   >>> int(Index())
   42
   >>> hex(Int())
   Traceback (most recent call last):
     Cell In[9], line 1
       hex(Int())
   TypeError: 'Int' object cannot be interpreted as an integer
   >>> hex(Index())
   '0x2a'
   
   >>> import math
   >>> math.factorial(Int())
   Traceback (most recent call last):
     Cell In[4], line 1
       math.factorial(Int())
   TypeError: 'Int' object cannot be interpreted as an integer
   
   >>> math.factorial(Index())
   1405006117752879898543142606244511569936384000000000
   ```
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscr...@arrow.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to