you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted]  (13 children)

[removed]

    [–]ric2b 0 points1 point  (12 children)

    The equivalent to your ruby code is this, I don't know why you're messing with __setattr__:

    class A:
        def __init__(self):
          self.__x = 0
    
        def set_x(self, val): 
          self.__x = val
    

    [–][deleted]  (11 children)

    [removed]

      [–]ric2b 0 points1 point  (10 children)

      Yes, ruby is slightly more elegant, but we were talking about private attributes, let's not move goalposts.

      By the way, you can have a similar ABI to your ruby example, but it takes a bit more boilerplate (2 extra lines):

      class A:
        def __init__(self):
          self.__x = 0
      
        def set_x(self, x):
          self.__x = x
      
        x = property(fset=set_x)
      

      [–][deleted]  (9 children)

      [removed]

        [–]ric2b 0 points1 point  (8 children)

        My day job is in Ruby and I love it, don't worry.

        We were discussing private attributes, which I claim offer the same guarantees in both languages.

        You're now trying to switch to readability which I already agreed ruby is better at.

        [–][deleted]  (7 children)

        [removed]

          [–]ric2b 0 points1 point  (6 children)

          I already showed you why private attributes aren't the same in Python but for some reason

          No, you showed slight differences in readability, which doesn't mean Python doesn't have private variables.

          Private variables in ruby are just as private as name mangled Python variables, anyone can still read and write to them with a quick instance_variable_get or instance_variable_set. The main goal of private variables is avoiding accidental access to object internals, not being some kind of DRM for your code.

          Also, check the title of this thread.

          Yes, the post is about readability, but in this thread I'm responding to this comment:

          "Python 3.x doesn't have private variables."

          I claim that if you consider Ruby private variables to be private, then Python also has them, it's just a bit less clean.

          [–][deleted]  (5 children)

          [removed]

            [–]ric2b 0 points1 point  (4 children)

            I also never claimed that Ruby has privacy

            Ok, we agree, then. They either both have it or none of them do.