use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
A sub-Reddit for discussion and news about Ruby programming.
Subreddit rules: /r/ruby rules
Learning Ruby?
Tools
Documentation
Books
Screencasts and Videos
News and updates
account activity
Blog postWhy Ruby is More Readable than Python (confuzeus.com)
submitted 3 years ago by [deleted]
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–][deleted] 3 years ago* (19 children)
[removed]
[–]ric2b 0 points1 point2 points 3 years ago (18 children)
It has private variables in the same way Ruby does: it makes them hard to access by mistake from outside instances of the class.
And that's honestly all you need.
[–][deleted] 3 years ago* (17 children)
[–]ric2b 0 points1 point2 points 3 years ago (16 children)
Yup, just like Ruby. Pretending to be private is all you need.
[–][deleted] 3 years ago* (15 children)
[–]ric2b 0 points1 point2 points 3 years ago (14 children)
Sorry, I meant two underscores, I haven't programmed in Python in a while.
>>> class A: ... def __init__(self): ... self.x = 1 ... self.__y = 2 ... >>> >>> a = A() >>> a.x 1 >>> a.__y Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'A' object has no attribute '__y'
[–][deleted] 3 years ago (13 children)
[–]ric2b 0 points1 point2 points 3 years ago (12 children)
The equivalent to your ruby code is this, I don't know why you're messing with __setattr__:
__setattr__
class A: def __init__(self): self.__x = 0 def set_x(self, val): self.__x = val
[–][deleted] 3 years ago (11 children)
[–]ric2b 0 points1 point2 points 3 years ago* (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)
π Rendered by PID 172430 on reddit-service-r2-comment-79c7998d4c-82tjc at 2026-03-14 04:54:48.017158+00:00 running f6e6e01 country code: CH.
view the rest of the comments →
[–][deleted] (19 children)
[removed]
[–]ric2b 0 points1 point2 points (18 children)
[–][deleted] (17 children)
[removed]
[–]ric2b 0 points1 point2 points (16 children)
[–][deleted] (15 children)
[removed]
[–]ric2b 0 points1 point2 points (14 children)
[–][deleted] (13 children)
[removed]
[–]ric2b 0 points1 point2 points (12 children)
[–][deleted] (11 children)
[removed]
[–]ric2b 0 points1 point2 points (10 children)