- E04 - Leadership! Google, Guido van Rossum, PSF
hans at zephyrfalcon.org
Tue Jan 3 19:46:13 CET 2006
Duncan Booth wrote:
> BTW, I don't know Ruby enough to understand the example at
> class Object
> def meta # adds variable "meta" to all objects in the system
I don't think this is valid Ruby code, by the way... It should probably
be something like this:
> Talker.meta = "Class meta information"
> john.meta = "Instance meta information"
> 1234.meta = 'any Instance meta information"
> puts Talker.meta
> puts john.meta
> puts 1234.meta # an integer object
> With the above code what would 'puts someexpressionresultingin1234.meta'
> output? i.e. is the attribute being set on all integers with the value
> 1234, or just on a specific instance of an integer.
At first glance, it seems the former is true:
irb(main):021:0> class Object
irb(main):022:1> attr_accessor :meta
irb(main):026:0> 1234.meta = "fred"
irb(main):028:0> x = 617
irb(main):029:0> x *= 2
However, inspecting the object_id (comparable to Python's id()) shows
that all these refer to the same object:
irb(main):041:0> y = 1000
irb(main):043:0> y += 234
I am not an expert on Ruby internals, but it looks like these integers
are cached. As with Python, I don't know if one can count on this
behavior to happen always.
More information about the Python-list