extending objects (object-specific subclassing)
David Garamond
davegaramond at icqmail.com
Sat Jan 25 08:37:51 EST 2003
in ruby you can "subclass an object". that is, you override and/or add
methods specific to an object, not for the entire class. an example below:
a = "hello"
b = a.dup # b becomes another String object with the value of "hello"
def b.to_s
"The value is '#{self}'"
end
puts a.to_s # hello
puts b.to_s # The value is 'hello'
behind the scene, ruby creates a new class just for the object a. the
original a's class, String, will be the new class' superclass. ruby
hides this, though, so you'll still see a as being the String object.
puts a.class # String
puts b.class # String
i wonder if python has a similar concept. or how do we do something
roughly similar in python.
--
dave
More information about the Python-list
mailing list