Come fa un int in pitone evitare essendo un oggetto ma tuttavia è uno:Python 2.7: Ints come oggetti
Se io il seguente:
>>> dir(10)
['__abs__', '__add__', '__and__', '__class__', '__cmp__', '__coerce__', '__delattr__', '__div__', '__divmod__', '__doc__', '__float__', '__floordiv__', '__format__', '__getattribute__', '__getnewargs__', '__hash__', '__hex__', '__index__', '__init__', '__int__', '__invert__', '__long__', '__lshift__', '__mod__', '__mul__', '__neg__', '__new__', '__nonzero__', '__oct__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdiv__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'bit_length', 'conjugate', 'denominator', 'imag', 'numerator', 'real']
>>> 10.__add__(20)
File "<stdin>", line 1
10.__add__(20)
^
SyntaxError: invalid syntax
Se digito 10. produce 10.0 mentre qualsiasi cosa come 10 .__ qualsiasi cosa __ produce un errore di sintassi. Ha senso dal momento che un float sarebbe considerato come 10.5 ma
- come viene raggiunto/implementato?
- come posso chiamare i metodi int su un int?
Cosa fa la parentesi sul lato sinistro in questo caso? Valuta l'espressione in un oggetto int? – Har
Immagino che si possa dire questo, ma lo si deve disambiguare da un float – ReyCharles
'(10.) __add __ (20)' e '10 .__ add __ (20)' è lo stesso. – ReyCharles