3 ms·
You can't add methods to integers in Python, but they're still objects in the sense that you can call methods on them: >>> i = 123 >>> i.bit_length() 7
by bakery2k 8y ago
You can't add methods to integers in Python, but they're still objects in the sense that you can call methods on them:
>>> i = 123
>>> i.bit_length()
7
- brudgers 8y agoSome methods but not others. Specifically, I can call built-in methods, but not methods I define. For example: >>> def f(self): ... return self ... >>> (3).f Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'int' object has no attribute 'f'
- bakery2k 8y agoYour `f` is not a method, but a top-level function whose first parameter happens to be named `self`. You can't call `f` as a method of any object, because in Python (unlike Ruby) the top-level namespace is unrelated to the namespaces of any classes.
- brudgers 8y agoI don't disagree. I just think it's much worse than that. There is no scope in Python where I can define 'f' to allow '(3).f'. I cannot restrict a function to the domain of Integers using encapsulation. I am forced to put it in some other arbitrary namespace.