a Python person's experience with Ruby

I V ivlenin at gmail.com
Sun Dec 9 16:10:21 EST 2007


On Sun, 09 Dec 2007 11:58:05 -0800, MonkeeSage wrote:
> class A
>   attr_accessor :a # == self.a,
>                    # accessible to instances of A
>   def initialize
>     @a = "foo" # A.__a
>                # only accessible from class scope of A
>   end
> end
> 
> Once again, there is no such thing as an attribute that is not a method
> in ruby, and there is no such thing as setting an *attribute* ("=" is a
> method also). You're trying to *re-implement* rubys mechanism in python
> in your example, but in pythons mechanism, all attributes already have
> built-in getter/setter. So the correct analogy is a variable that is
> accessible from within the classes scope only (A.__a), and then exposed
> to instances through an attribute (self.a). Your example leaves out a
> variable accessible only from within the scope of the class, and adds a
> new attribute accessible from the instance (_a).

Either I'm not understanding you, or you're not understanding what A.__a 
means in python. A.__a is a class attribute, not an instance attribute - 
it's equivalent to @@a in ruby, not @a. If I understand what you're 
saying, a better python example to make your point might be:

class A(object):
	def __init__(self):
		self.__a = "foo" # equivalent to @a
		self.a = self.__a # exposes self.__a

Except that this only allows you to access '__a' via the exposed 
attribute 'a', not bind it to a new object. If you do:

inst = A()
inst.a = "bar"

you don't modify inst.__a, unlike the following ruby code, which does 
modify @a .

inst = A.new
inst.a = "bar"



More information about the Python-list mailing list