Say I have a custom class Box
:
class Box(object):
pass
I try to add an attribute prop
:
box = Box()
box.prop = 1 # works
print(box.prop) # 1
But if I do the same thing on an int
object:
num = 1
num.prop = 1
Result
AttributeError: 'int' object has no attribute 'prop'
Questions:
- How to explain behavioural difference between
num
andbox
? - How can I make a custom class (Box2) behave like an
int
, i.e. to raiseAttributeError
afterbox2 = Box2(); box2.prop = 1
?