DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
Behaviour Of Inherited Class Variables In Python Vs Ruby
IMHO the way inherited class variables behave in python (and many other languages) makes much more sense than
the unexpected way they get modified on the base class in ruby.
This python code:
class Vehicle:
ENGINES = 1
WHEELS = None
doors = None
headlights = None
brakelights = None
def __init__(self,color=None):
self.color = color
def __str__(self):
return "This %s \t %s \t has %s engines, %s doors, %s headlights, %s brakelights and %s wheels." % (self.color, self.__class__.__name__, self.ENGINES, self.doors, self.headlights, self.__class__.brakelights, self.WHEELS)
class Car(Vehicle):
WHEELS = 4
doors = 4
headlights = 2
brakelights = 2
class Motorbike(Vehicle):
WHEELS = 2
doors = 0
headlights = 1
brakelights = 1
v = Vehicle()
c = Car('red')
m = Motorbike('green')
print v
print c
print m
class Ute(Car):
WHEELS = 4
doors = 2
print
u = Ute('blue')
print v
print c
print m
print u
Outputs:
This None Vehicle has 1 engines, None doors, None headlights, None brakelights and None wheels. This red Car has 1 engines, 4 doors, 2 headlights, 2 brakelights and 4 wheels. This green Motorbike has 1 engines, 0 doors, 1 headlights, 1 brakelights and 2 wheels. This None Vehicle has 1 engines, None doors, None headlights, None brakelights and None wheels. This red Car has 1 engines, 4 doors, 2 headlights, 2 brakelights and 4 wheels. This green Motorbike has 1 engines, 0 doors, 1 headlights, 1 brakelights and 2 wheels. This blue Ute has 1 engines, 2 doors, 2 headlights, 2 brakelights and 4 wheels.
Where similar code in ruby:
class Vehicle
ENGINES = 1
WHEELS = nil
@@doors = nil
@headlights = nil
class << self; attr_accessor :brakelights end
@brakelights = nil
def initialize(color=nil)
@color = color
end
def to_s
return "This %s \t %s \t has %s engines, %s doors, %s headlights, %s brakelights and %s wheels." % [@color, self.class.name, ENGINES, @@doors, @headlights, self.class.brakelights, WHEELS]
end
end
class Car < Vehicle
WHEELS = 4
@@doors = 4
@headlights = 2
@brakelights = 2
end
class Motorbike < Vehicle
WHEELS = 2
@@doors = 0
@headlights = 1
@brakelights = 1
end
v = Vehicle.new()
c = Car.new('red')
m = Motorbike.new('green')
puts v
puts c
puts m
class Ute < Car
WHEELS = 4
@@doors = 2
end
puts
u = Ute.new('blue')
puts v
puts c
puts m
puts u
Outputs:
This Vehicle has 1 engines, 0 doors, headlights, brakelights and wheels. This red Car has 1 engines, 0 doors, headlights, 2 brakelights and wheels. This green Motorbike has 1 engines, 0 doors, headlights, 1 brakelights and wheels. This Vehicle has 1 engines, 2 doors, headlights, brakelights and wheels. This red Car has 1 engines, 2 doors, headlights, 2 brakelights and wheels. This green Motorbike has 1 engines, 2 doors, headlights, 1 brakelights and wheels. This blue Ute has 1 engines, 2 doors, headlights, brakelights and wheels.
Note 1: The way that the class definitions modify the base classes class variables.
eg. defining a Car changes the Vehicle's DOORS value from nil to 4, then defining a Motorbike
changes it from 4 to 0, then defining a Ute changes it again from 4 to 2.
(IMHO this doesn't make any sense)
Note 2: The most usable form of class variable in this example was the "brakelights". It was made
as an instance variable on the class. Unfortunately though, in this example the "Ute" class
doesn't inherit it as you'd expect.






Comments
Snippets Manager replied on Tue, 2009/03/03 - 10:55am