Re: [Tutor] Accessing class attributes: use methods only?
Thanks very much for your responses, all. Just to clarify, yes, by through class methods I actually meant through methods of instances of a class. Now for more discussion: I'm confused. On the one hand we have Mr. Kent Johnson's statement: On 2/13/07, Kent Johnson [EMAIL PROTECTED] wrote: Python practice is to use direct attribute access. If you need to do something special when an attribute is read or set, you can create a property. This allows you to define methods to be called when the attribute is accessed, without having to change the client code. So to paraphrase, he states it's the Python way to do: my_instance = MyClass() my_instance.x = 42 On the other hand, we have Mr. Alan Gauld, who states: On 2/13/07, Alan Gauld [EMAIL PROTECTED] wrote: Its generally good OOP practice to interact with object via messages. Its also good practice NOT to access an objects attributes directly (and that includes via get/set methods) A class should publish a set of operations. The attributes should be there to support those operations. So to paraphrase, he states it's the right way, regardless of language, to do: my_instance = MyClass() my_instance.setx(42) I'm used to just setting and getting attributes straight, which would be Pythonic according to Kent and yet also outright wrong according to Alan and academic papers. So is direct access actually not Pythonic, or is it Pythonic and Pythonistas spit in the face of Demeter and her lovely laws? Curious, Chris ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Accessing class attributes: use methods only?
Ah ha! All is clear, now. I understand what I misinterpreted in your first post, Alan. Thanks also to Lloyd for reinforcing the concept. Much appreciated! Chris ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Accessing class attributes: use methods only?
Is it general good practice to access and set class attributes via methods only, or is it okay practice to directly interact with class attributes? The professor in a class on Perl that I'm taking suggested that directly accessing and setting class attributes was a bad idea. Just wondering what the current preference in Python is. Many thanks, Chris ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] What is a mixin class?
On 1/17/07, Don Taylor [EMAIL PROTECTED] wrote: So, what constitutes a mixin class and what are the conventional ways to denote them in code? A mixin is a specific type of superclass, just called a mixin because of the concept it represents. A common type of mixin would be a class that defines some sort of modified functionality intended to be given to multiple, not necessarily related classes. Say you wanted all your classes to have a similar looking format when printed. You could define a mixin class that defined a special __repr__ method. A detailed example of this is demonstrated in /Learning Python/ by Lutz Ascher. There's no need to explicitly state that they are a mixin class, but if you felt the need to, you could either put it in a comment or, probably even better, in the mixin's docstring. Chris ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] OT: Vim was: free IDE for Python?
On 11/17/06, Mike Hansen [EMAIL PROTECTED] wrote: Here's what I'm doing. Not sure if it's that helpful to you. I use the mini-buffer explorer plug-in and the taglist plugin. set smartindent shuts off the annoying # comment in smartindent to jump to col 1 inoremap # Xc-h# autocmd BufRead *.py set smartindent cinwords=if,elif,else,for,while,try,except,finally,def,class Instead of using smartindent + cinwords + the inoremap hack, simply use filetype indent on. This was recommended to me on #Vim IRC channel. This takes care of the silly # problem with smartindent and is considered the best way for getting proper indentation in Python. Give it a try! Chris ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Equivalent to perl -e
My professor and advisor has been inspired by me to give Python a try. He's an avid Perl user, and challenged me with the following: What is the Python equivalent to perl -e 'some oneliner'? Embarassingly, I had no answer, but I figure, someone on the list will know. His use of Python is at stake; he threatened that, since he's dependant enough on using perl -e within Emacs enough, if it can't be done in Python, he won't take the language seriously. Help me, Python Tutor, you're his only hope! Thanks in advance, Chris ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Equivalent to perl -e
Haha! I'll relay that message! Thanks Kent and Glenn! Chris On 10/15/06, Glenn T Norton [EMAIL PROTECTED] wrote: Chris Lasher wrote: My professor and advisor has been inspired by me to give Python a try. He's an avid Perl user, and challenged me with the following: What is the Python equivalent to perl -e 'some oneliner'? Embarassingly, I had no answer, but I figure, someone on the list will know. His use of Python is at stake; he threatened that, since he's dependant enough on using perl -e within Emacs enough, if it can't be done in Python, he won't take the language seriously. Help me, Python Tutor, you're his only hope! Thanks in advance, Chris ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor How about... python -c for x in 'Tell them to jump on board and take the blue pill': print x Glenn -- Ketchup. For the good times... - Ketchup Advisory Board Glenn Norton Application Developer Nebraska.gov 1-402-471-2777 [EMAIL PROTECTED] ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Equivalent to perl -e
Points well taken. In fact, the example he demonstrated to me as a one-liner was a regular expression as a line filter in Emacs--essentially just a grep. There's no Pythonic equivalent to this. Right tool for the right job, as you said. He was half-joking about not learning Python if it lacked the option to execute snippets. His lab maintains a significant amount of Perl code; he was intrigued by my zealous enthusiasm for Python and my assertion that, personally, I experienced greater long-term readability with my scripts written in Python over those written in Perl. I think once he begins to experience Python he will come to understand why it's not suited for one-liners, and why that's a Good Thing. Excellent reply! Chris On 10/15/06, Tim Peters [EMAIL PROTECTED] wrote: [Chris Lasher] My professor and advisor has been inspired by me to give Python a try. He's an avid Perl user, and challenged me with the following: What is the Python equivalent to perl -e 'some oneliner'? The initally attractive but unsatisfying answer is: python -c 'some oneliner' The reason it's unsatisfying is that Python isn't concerned with making: some oneliner pleasant, or even sanely possible, for many tasks. Perl excels at one-liners; Python doesn't much care about them. Embarassingly, I had no answer, but I figure, someone on the list will know. His use of Python is at stake; he threatened that, since he's dependant enough on using perl -e within Emacs enough, if it can't be done in Python, he won't take the language seriously. Help me, Python Tutor, you're his only hope! Like many Python (very) old-timers, I used Perl heavily at the time Python came out. As was also true for many of them, as time went on the size of a new program I was willing to write in Perl instead of in Python got smaller and smaller, eventually reaching almost 0. I still use Perl some 15 years later, but now /only/ for perl -e-style 1-liners at an interactive shell. If it takes more than a line, I stick it in a module (and maybe a class) for reuse later. Python's strengths are more in readability, helpful uniformity, easy use of classes and rich data structures, and maintainability. Cryptic one-liners are in general (but not always) opposed to all of those. So, ya, python -c exists, but your professor won't be happy with it. That's fine! If one-liners are all he cares about, Perl is usually the best tool for the job. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor