-2

i've read Clean Code in Python book by Mariano Anaya and in this book he said when we use leading double underscore the attribute goes private and you can't call the attribute or change value of attribute enter image description here but in my code i can change private attribute without get error but why? my version of my python is 3.10enter image description here

i 've tried both leading single underscore and leading double underscore and not any error raising

Liam
  • 11
  • 3
  • 1
    The leading underscores are merely a convention. You still *can* call the attribute or access or change its value, you just *shouldn't* - we are all responsible users who don't do what we shouldn't do, right? Note that even in languages where you *can't* usually do it (e.g. Java) there are typically ways to bypass it (e.g. using reflection). – Luatic Aug 12 '23 at 22:32
  • 1
    Private attributes are not a security mechanism but rather an aid for the programmer to mark internals that shouldn't be relied upon, making them easier to change. If someone's code is running outside of a carefully designed sandbox, it typically can do everything your code can do anyways. – Luatic Aug 12 '23 at 22:34
  • @Luatic double leading underscores are more than convention in Python, see dupe. – Mark Aug 12 '23 at 22:35
  • ok but why in his book put picture of shell that raises error when he use double under score i upload picture n my question @Luatic – Liam Aug 12 '23 at 22:41
  • 1
    Do you see when you print your Car instance the first time the property has changed to `'_Car__speed':` not `__speed`. Try `c = Car ("red")` followed by `print(c.__speed)`. You create a *new* property when you do `c. __speed = 30` you don't overwrite the original. – Mark Aug 12 '23 at 22:44
  • 2
    That is simply incorrect. Python doesn't have private attributes. Double-underscore name mangling is for preventing accidental name collisions in subclasses, it is *not* for preventing access – juanpa.arrivillaga Aug 12 '23 at 22:47
  • 1
    And reading from the image, it seems the author is trying to explain precisely that! – juanpa.arrivillaga Aug 12 '23 at 22:48
  • _"he said when we use leading double underscore the attribute goes private"_. Doesn't the passage in the image say, literally, that that's a misconception? – Brian61354270 Aug 12 '23 at 22:51
  • _" but in my code i can change private attribute"_ actually, you didn't. Look more closely at the dict you printed. – Brian61354270 Aug 12 '23 at 22:53
  • The double underscore name mangling is designed to allow a parent class to prevent subclasses from accidentally overriding certain properties. It's not a security mechanism, but it's also not pure convention to communicate with developers since the language actively changes behavior based on their presence. – Mark Aug 12 '23 at 22:55
  • @juanpa.arrivillaga traceback error occurs because he enforces code in shell python ? and pycharm or vscode ignore this ? i 've confused why raising error for his code? – Liam Aug 12 '23 at 23:00
  • 1
    It is raising in error in his code because he is trying to access a non-existing property. Look at my comment above, `print(c.__speed)` before you redefine it and you will get an error too. – Mark Aug 12 '23 at 23:02
  • 1
    @Mark oh i figure out this finally thank a lot – Liam Aug 12 '23 at 23:07

0 Answers0