Skip to main content
Stack Overflow
  1. About
  2. For Teams

Timeline for How to do encapsulation in Python?

Current License: CC BY-SA 3.0

24 events
when toggle format what by license comment
Jul 11, 2021 at 12:32 answer added Sam Y timeline score: 0
Jul 19, 2018 at 17:24 comment added Mr. Lance E Sloan This is stronger enforcement of class member access rather than encapsulation. Could this be done with introspection rather than using on self.privates and self.protected? Follow the common Python style of naming variables __a and _b. Then the methods would check whether the names begin with __ or _ to allow or deny access.
Dec 3, 2015 at 18:24 comment added Heath Hunnicutt @will, who cares? In C, the private members can be accessed with pointer tricks. Every mechanism has its backdoor, and you aren't concerned with people who need to deliberately bypass your encapsulation. The purpose of what you've implemented is to prevent casual use of private members, in order to avoid the implementation of bugs. That's the purpose of encapsulation, and your code accomplishes it.
Dec 3, 2015 at 12:08 comment added will @HeathHunnicutt It does not really though, since the methods can be overwritten to remove the protection.
Dec 3, 2015 at 8:38 history reopened Heath Hunnicutt
Bartłomiej Semańczyk
Wtower
Jaap
Reeno
Dec 3, 2015 at 8:06 review Reopen votes
Dec 3, 2015 at 8:38
Dec 3, 2015 at 7:48 comment added Heath Hunnicutt @will I think it's wonderful for the occasions that you really want encapsulation. Python allows it.
Sep 3, 2015 at 23:17 review Reopen votes
Sep 3, 2015 at 23:54
Sep 3, 2015 at 22:54 history edited kenorb CC BY-SA 3.0
Focusing question by making it clearer, so it can be considered for re-open.
Dec 6, 2014 at 17:34 history closed jonrsharpe
piokuc
Veedrac
Diosney
davidism
Needs details or clarity
Oct 6, 2014 at 14:52 comment added will @Ben i guess my question is would it even be possible to do do this? It's purely curiosity. I can protect against redefining sys._getframe by reloading sys, i can protect against someone subclassing it and then refdefining __get/set... by making the class final as done here, but i can't think of any way to truly make it so it can't be broken.
Oct 6, 2014 at 14:40 comment added Ben @will That's kind of our question too - how far down the rabbit hole do you go to stop people doing this? What's the point?
Oct 6, 2014 at 14:14 comment added will @Ben that can be prevented by adding "__getattribute__" and "__setattr__" to the privates` array though. And sure, then you could evade it by doing sys._getframe = lambda x: type("a", (object,), dict(f_code=type("a", (object,), dict(co_argcount=1)))), but how far down this rabbit hole do you go?
Oct 6, 2014 at 13:48 comment added Ben You can never stop people doing whatever they like in your class. What happens if I do EncapsulationClass.__getattribute__ = .... or EncapsulationClass.__setattr__ = .... to rewrite them to more 'standard' versions?
Oct 6, 2014 at 13:11 review Close votes
Dec 6, 2014 at 17:34
Oct 6, 2014 at 13:10 answer added Flavian Hautbois timeline score: 1
Oct 6, 2014 at 13:08 answer added mhawke timeline score: 48
Oct 6, 2014 at 13:07 history edited will CC BY-SA 3.0
added 145 characters in body
Oct 6, 2014 at 13:01 comment added will @DanielRoseman - you break it! I didn't spot that one...
Oct 6, 2014 at 12:59 comment added Daniel Roseman I wouldn't call code that accesses sys._getframe(1).f_code.co_argcount "simple", but never mind. What happens if I do EncapsulationClass.protected = [] from my code?
Oct 6, 2014 at 12:57 comment added will It seems to work fine, yes, but i so often see people saying "python does not have encapsulation", so presumed it couldn't be as simple as this.
Oct 6, 2014 at 12:56 comment added user3058846 Could've asked in codereview
Oct 6, 2014 at 12:53 comment added jonrsharpe ...what do you mean? What do you think is wrong with it? Does it run?
Oct 6, 2014 at 12:48 history asked will CC BY-SA 3.0
toggle format

AltStyle によって変換されたページ (->オリジナル) /