tag:blogger.com,1999:blog-8935780327334775165.post1111242154257946396..comments2023-08-07T22:48:57.800+08:00Comments on Entity Crisis: Public and Private in Python?Unknownnoreply@blogger.comBlogger17125tag:blogger.com,1999:blog-8935780327334775165.post-92231070551057795502011-05-10T03:09:09.608+08:002011-05-10T03:09:09.608+08:00You can separate interface and implementation usin...You can separate interface and implementation using closures. To me this seems much less natural in Python compared to JavaScript where it's the norm. I would like a better syntax for this.<br /><br />def make_class():<br /> global X<br /><br /> def private_method(self): pass<br /><br /> class X(object):<br /> def public_method(self):<br /> private_method(self)Danielhttps://www.blogger.com/profile/13890648911094881608noreply@blogger.comtag:blogger.com,1999:blog-8935780327334775165.post-83303053307038138912011-04-28T00:15:58.743+08:002011-04-28T00:15:58.743+08:00s/EAFB/EAFPs/EAFB/EAFPAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-8935780327334775165.post-25082036495280780762011-04-28T00:14:48.909+08:002011-04-28T00:14:48.909+08:00This isn't about keywords on implementations, ...This isn't about keywords on implementations, this is about interfaces, right? Most Python software doesn't need explicit public interfaces. And those that do use zope.interface. I don't mean to be so blunt, but after years of using one of the following two strategies, it seems a non-problem; either: (1) duck-type and use data-normalization decorators around your public methods (e.g. guarantee your REST API can give your method that needs strings a unicode value without issue; move the EAFB and normalization into a decorator for more elegant, readable methods); (2) use zope.interface and zope.component. You can use both.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8935780327334775165.post-57161580321673934562011-04-27T23:49:18.103+08:002011-04-27T23:49:18.103+08:00This comment has been removed by the author.eryksunhttps://www.blogger.com/profile/15437401811057805475noreply@blogger.comtag:blogger.com,1999:blog-8935780327334775165.post-9158169888547946502011-04-27T22:32:15.085+08:002011-04-27T22:32:15.085+08:00__slots__ ?
But until today, nobody convinced me ...__slots__ ?<br /><br />But until today, nobody convinced me why make attributes and methods really privateAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-8935780327334775165.post-87063853461415046872011-04-27T21:39:14.368+08:002011-04-27T21:39:14.368+08:00Good points. I'm thinking this completely from...Good points. I'm thinking this completely from the Java point of view so I missed this Python philosophy.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8935780327334775165.post-20383517217829532362011-04-27T21:27:49.156+08:002011-04-27T21:27:49.156+08:00First: Python is not only procedural...
Second: Py...First: Python is not only procedural...<br />Second: Python has a philosophie, and it defined the language. Thats the main reason to avoid the use of keywords like private, static, etc.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8935780327334775165.post-11446225273779244592011-04-27T21:01:06.511+08:002011-04-27T21:01:06.511+08:00if you have private and public then you should add...if you have private and public then you should add protected and internal and there are still situations when all the 4 options are not good enough. It just adds a lot of complexity in order to get more control. Learn give up control you control freaks!duraidhttps://www.blogger.com/profile/08887287650549815276noreply@blogger.comtag:blogger.com,1999:blog-8935780327334775165.post-25707335921878652612011-04-27T15:09:28.012+08:002011-04-27T15:09:28.012+08:00I must agree with Simon, underscore prefix is ugly...I must agree with Simon, underscore prefix is ugly. I can't understand why there isn't "real" private scope, it feels so fundamental when thinking about encapsulation. <br /><br />Though I don't have much experience with procedural languages.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8935780327334775165.post-86189756939831854412011-04-27T11:52:10.303+08:002011-04-27T11:52:10.303+08:00Then you should have forgotten all of your C# beca...Then you should have forgotten all of your C# because it's so ugly.<br /><br />Actually, it's so much more readable and explicite than a keyword in the declaration.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8935780327334775165.post-69672726240978393282011-04-27T11:06:59.546+08:002011-04-27T11:06:59.546+08:00Right, I had forgotten about the underscore prefix...Right, I had forgotten about the underscore prefix system. I guess it fell from memory due to lack of use, because it is so ugly!Simon Wittberhttps://www.blogger.com/profile/02730025645144151014noreply@blogger.comtag:blogger.com,1999:blog-8935780327334775165.post-23867161126894758962011-04-27T07:14:43.328+08:002011-04-27T07:14:43.328+08:00>>> class A:
... def __private(self):...>>> class A:<br />... def __private(self):<br />... return 2<br />... def public(self):<br />... return self.__private()<br />... <br />>>> a = A()<br />>>> a.__private()<br />Traceback (most recent call last):<br /> File "", line 1, in <br />AttributeError: A instance has no attribute '__private'<br />>>> a.public()<br />2Adamnoreply@blogger.comtag:blogger.com,1999:blog-8935780327334775165.post-38163047615686405472011-04-27T04:35:26.069+08:002011-04-27T04:35:26.069+08:00Please see the tutorial at http://docs.python.org/...Please see the tutorial at <a rel="nofollow">http://docs.python.org/tutorial/classes.html#tut-private</a> for more information about this.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8935780327334775165.post-34193138591472642702011-04-27T02:58:55.407+08:002011-04-27T02:58:55.407+08:00I know it's helpful. So is static typing, it&#...I know it's helpful. So is static typing, it's extremely helpful when refactoring legacy code. But that's not what python is about which simplicity and minimalism.<br /><br />If you want to be able to refactor python code easily you need to make sure you have good unit tests.duraidhttps://www.blogger.com/profile/08887287650549815276noreply@blogger.comtag:blogger.com,1999:blog-8935780327334775165.post-15871582335353173532011-04-27T02:34:08.544+08:002011-04-27T02:34:08.544+08:00In addition to the under and dunder, you can use t...In addition to the under and dunder, you can use things like properties to refactor a seemingly public variable to act like a method.<br /><br />I agree that the model is helpful.elarsonhttps://www.blogger.com/profile/10311421080197766290noreply@blogger.comtag:blogger.com,1999:blog-8935780327334775165.post-10016263682359944742011-04-27T02:17:57.220+08:002011-04-27T02:17:57.220+08:00Just use the standard Python convention that attri...Just use the standard Python convention that attributes / methods starting with an underscore are private.Michael Foordhttps://www.blogger.com/profile/06229713779852499022noreply@blogger.comtag:blogger.com,1999:blog-8935780327334775165.post-17548375373910024142011-04-27T02:02:31.728+08:002011-04-27T02:02:31.728+08:00We do.
It's just that if you *need* to, you ...We do. <br /><br />It's just that if you *need* to, you can access the private classes too. If you do that with your own library, then you probably need to refactor it. If you do it with somebody elses, you have to count on it breaking when he refactors the code.<br /><br />The lack of an actual "private" keyword doesn't stop you from separating your code into a public API and internal implementation.Lennart Regebrohttps://www.blogger.com/profile/08337807480455483637noreply@blogger.com