Can someone show me how to code this correctly please<br><br><div class="gmail_quote">On Wed, Jun 22, 2011 at 10:00 PM, <span dir="ltr"><<a href="mailto:tutor-request@python.org">tutor-request@python.org</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">Send Tutor mailing list submissions to<br>
<a href="mailto:tutor@python.org">tutor@python.org</a><br>
<br>
To subscribe or unsubscribe via the World Wide Web, visit<br>
<a href="http://mail.python.org/mailman/listinfo/tutor" target="_blank">http://mail.python.org/mailman/listinfo/tutor</a><br>
or, via email, send a message with subject or body 'help' to<br>
<a href="mailto:tutor-request@python.org">tutor-request@python.org</a><br>
<br>
You can reach the person managing the list at<br>
<a href="mailto:tutor-owner@python.org">tutor-owner@python.org</a><br>
<br>
When replying, please edit your Subject line so it is more specific<br>
than "Re: Contents of Tutor digest..."<br>
<br>
<br>
Today's Topics:<br>
<br>
1. Re: Class methods (Alan Gauld)<br>
2. Re: sftp get single file (Peter Lavelle)<br>
<br>
<br>
----------------------------------------------------------------------<br>
<br>
Message: 1<br>
Date: Wed, 22 Jun 2011 08:58:03 +0100<br>
From: "Alan Gauld" <<a href="mailto:alan.gauld@btinternet.com">alan.gauld@btinternet.com</a>><br>
To: <a href="mailto:tutor@python.org">tutor@python.org</a><br>
Subject: Re: [Tutor] Class methods<br>
Message-ID: <its7af$h8k$<a href="mailto:1@dough.gmane.org">1@dough.gmane.org</a>><br>
Content-Type: text/plain; format=flowed; charset="UTF-8";<br>
reply-type=original<br>
<br>
"David Merrick" <<a href="mailto:merrickdav@gmail.com">merrickdav@gmail.com</a>> wrote<br>
<br>
> class Critter(object):<br>
><br>
> def __init__(self, name, hunger = 0, boredom = 0):<br>
> def __pass_time(self):<br>
> def __str__(self):<br>
> @property<br>
> def mood(self):<br>
> def talk(self):<br>
> def eat(self):<br>
> def play(self):<br>
><br>
> class Farm(Critter):<br>
<br>
I still don't think a Farm is a type of Critter...<br>
<br>
> def __init__(self,farmlet):<br>
> Critter.__init__(self,farmlet)<br>
<br>
This will set the name to farmlet, which I don't<br>
think you want.<br>
<br>
> def talk(self,farmlet):<br>
<br>
You don't need to pass farmlet in since the<br>
class has farmlet stored inside it.<br>
You can access farmlet with self.farmlet.<br>
<br>
> for critter in farmlet:<br>
> print("Hello")<br>
> Critter.talk(farmlet)<br>
<br>
You want the instance to talk not the class.<br>
So you need to use critter.talk() And the talk<br>
method does not take any arguments except<br>
self. What you are doing here is calling the<br>
class method with an instance value of farmlet.<br>
ie self in that method gets the value of farmlet.<br>
<br>
> def main():<br>
> crit1 = Critter("Sweetie")<br>
> crit2 = Critter("Dave")<br>
> farmlet = [crit1,crit2]<br>
> f = Farm(farmlet)<br>
><br>
> choice = None<br>
> while choice != "0":<br>
> print \<br>
> ("""<br>
> Critter Caretaker<br>
><br>
> 0 - Quit<br>
> 1 - Listen to your critter<br>
> 2 - Feed your critter<br>
> 3 - Play with your critter<br>
> """)<br>
><br>
> choice = input("Choice: ")<br>
> print()<br>
><br>
> # exit<br>
> if choice == "0":<br>
> print("Good-bye.")<br>
><br>
> # listen to your critter<br>
> elif choice == "1":<br>
> f.talk(farmlet)<br>
><br>
> # feed your critter<br>
> elif choice == "2":<br>
> f.eat(farmlet)<br>
<br>
Note that f.eat is a method you inherit from Critter.<br>
The Critter method does not take any arguments<br>
so this will fail.<br>
<br>
> # play with your critter<br>
> elif choice == "3":<br>
> f.play(farmlet)<br>
<br>
Same with f.play()<br>
<br>
> Traceback (most recent call last):<br>
> File "D:/David/Python/programs/critter_farm3.py", line 72, in talk<br>
> Critter.talk(farmlet)<br>
> File "D:/David/Python/programs/critter_farm3.py", line 38, in talk<br>
> print("I'm", <a href="http://self.name" target="_blank">self.name</a>, "and I feel", self.mood, "now.\n")<br>
> AttributeError: 'list' object has no attribute 'name'<br>
<br>
This is because you are accessing the method via<br>
the class and passing farmlet as the instance value<br>
rather than sending the message to the innstance<br>
directly. Use<br>
<br>
critter.talk() # and no farmlet needed!<br>
<br>
HTH,<br>
<br>
<br>
--<br>
Alan Gauld<br>
Author of the Learn to Program web site<br>
<a href="http://www.alan-g.me.uk/" target="_blank">http://www.alan-g.me.uk/</a><br>
<br>
<br>
<br>
<br>
------------------------------<br>
<br>
Message: 2<br>
Date: Wed, 22 Jun 2011 10:07:53 +0100<br>
From: Peter Lavelle <<a href="mailto:lists@solderintheveins.co.uk">lists@solderintheveins.co.uk</a>><br>
To: <a href="mailto:tutor@python.org">tutor@python.org</a><br>
Subject: Re: [Tutor] sftp get single file<br>
Message-ID: <<a href="mailto:4E01B0E9.8000300@solderintheveins.co.uk">4E01B0E9.8000300@solderintheveins.co.uk</a>><br>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed<br>
<br>
You could use the subprocess module to run the relevant system commands.<br>
More info on running sftp non-interactively (i.e from a script) can be<br>
found here: <a href="http://fixunix.com/ssh/238284-non-interactive-sftp-put.html" target="_blank">http://fixunix.com/ssh/238284-non-interactive-sftp-put.html</a><br>
<br>
Regards<br>
<br>
Peter Lavelle<br>
<br>
<br>
------------------------------<br>
<br>
_______________________________________________<br>
Tutor maillist - <a href="mailto:Tutor@python.org">Tutor@python.org</a><br>
<a href="http://mail.python.org/mailman/listinfo/tutor" target="_blank">http://mail.python.org/mailman/listinfo/tutor</a><br>
<br>
<br>
End of Tutor Digest, Vol 88, Issue 89<br>
*************************************<br>
</blockquote></div><br><br clear="all"><br>-- <br>Dave Merrick<br><br><a href="mailto:merrickdav@gmail.com">merrickdav@gmail.com</a><br><br>Ph 03 3423 121<br>Cell 027 3089 169<br>