a Python person's experience with Ruby

I V ivlenin at gmail.com
Sat Dec 8 16:14:03 EST 2007


On Sat, 08 Dec 2007 11:23:57 -0800, MonkeeSage wrote:
>> > The equivalent python idiom is something like:
>>
>> > class A:
>> >   __a = "foo"
>> >   def __init__(self):
>> >     self.a = A.__a
[...]
>> > Which roughly translates to this in ruby:
>>
>> > class A
>> >   attr_accessor :a
>> >   def initialize
>> >     @a = "foo"
>> >   end
>> > end
[...]
> Not really. In ruby an ivar is accessible within the class *only*, but
> not from without (like a mangled python class var), unless you declare
> an accessor (or write the accessor methods yourself). So my example is
> closer, and is not a WTF, if you know how ruby works.

In your python example, the class attribute is mangled, but the instance 
attribute isn't, whereas your ruby code has no class attribute, and an 
instance attribute that isn't (directly) accessible outside the class. 
The equivalent in python would involve a mangled instance attribute,  
like:

class A(object):
	def __init__(self):
		self.__a = "foo"
	def get_a(self):
		return self.__a
	def set_a(self, val):
		self.__a = val
	a = property(get_a, set_a)



More information about the Python-list mailing list