On 12/05/2012 10:38 AM, Malcolm Newsome wrote: > Hey Tutors, > > Python is/was my first language. Yet, I've recently begun learning C# for > my new job. > > One thing I've come across in C# (and, quite frankly, am having a difficult > time grasping) is Get and Set (Accessors). > > Since, I don't ever recall reading about this in Python, I'm wondering if > they exist. If not, what is the "thinking" behind why they are not > included in the language?
I don't know C#, but it's probably quite similar to Java in these things. Python doesn't need get and set, because it has a better mechanism. The whole idea of get and set in java is that a user of a class shouldn't make any assumption about internal details of a class. The user calls these two functions to read and write a property. Now, if the implementor changes his mind about what data format is actually used, he can still implement the original get and set function interfaces in terms of the new data fields. So, you might ask, how does the implementor decide which fields need such abstraction, and which ones can just be used, safe from likely change. Java's answer is that all fields should be wrapped in the getter and setter paradigm. In Python, the user of a class uses the data fields in the obvious way. Then when the implementor decides that a particular field needs "abstraction," he doesn't have to make the user change his use. Instead the implementor defines those get and set functions, and binds them to look like a simple field. That is most easily done by the @property decorator. > Additionally, a separate, but perhaps related question is that I have not > seen public/private classes in Python. How might this factor into the > whole accessor scenario? (Or, am I trying to relate two topics that have > nothing to do with each other?) > > Python doesn't have any such thing as private classes or private attributes. And it doesn't have friends, protected inheritance, etc. Instead it's a naming convention, that nobody is supposed to use any names with a leading underscore. The theory is that we're all adults here. Many times in C++ and Java, you need to cheat the private/protected schemes, to get something tricky accomplished. One time is when you're using a library for which you don't have permission to modify the source of a library. Another place is in implementing a library, where you need to follow different rules than the user of the library. In Python, instead of cheating (eg. by casting), you just access the item. I see that Francois has responded while I'm composing this. Please read that response as well as mine. -- DaveA _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor