Re: [Tutor] Class methods

2011-06-27 Thread Alan Gauld
"David Merrick" wrote Is it possible too have crit1 = Critter("Dave") crit2 = Critter("Sweetie") farm = [crit1,crit2] #List# and then be able to use Critters methods on farm? No, Marc has already answered that. class Critter(object): """A virtual pet""" def __init__(self, name, hu

Re: [Tutor] Class methods

2011-06-26 Thread Marc Tompkins
On Sun, Jun 26, 2011 at 7:12 PM, David Merrick wrote: > Is it possible too have > > crit1 = Critter("Dave") > crit2 = Critter("Sweetie") > farm = [crit1,crit2] #List# > > and then be able to use Critters methods on farm? > > No. farm is a list, and lists don't inherit the methods of the objects

[Tutor] Class methods

2011-06-26 Thread David Merrick
Is it possible too have crit1 = Critter("Dave") crit2 = Critter("Sweetie") farm = [crit1,crit2] #List# and then be able to use Critters methods on farm? # Critter Caretaker # A virtual pet to care for class Critter(object): """A virtual pet""" def __init__(self, name, hunger = 0, bore

Re: [Tutor] Class methods

2011-06-22 Thread Alan Gauld
"David Merrick" wrote Can someone show me how to code this correctly please? We've been doing that but you are still making some very basic mistakes which reflect a deep misunderastanding of what you are doing. You really should take several steps back and review the use of variables and fun

Re: [Tutor] Class methods

2011-06-22 Thread Alan Gauld
"michael scott" wrote you are using python 3 by your print statements, so I don't think you need the int() around your input, Yes he does because in Python 3 input is the same as raw_input in Python 2 even in python.2x input() was safe for numbers I believe (the whole list will rip my

Re: [Tutor] Class methods

2011-06-22 Thread michael scott
t: Wednesday, June 22, 2011 6:48 PM Subject: Re: [Tutor] Class methods David, 2011/6/22 David Merrick : >     # listen to your critter >     elif choice == "1": >     for critter in farmlet: >     farmlet.talk() You want to call .talk() on your "

Re: [Tutor] Class methods

2011-06-22 Thread Alexandre Conrad
David, 2011/6/22 David Merrick : >     # listen to your critter >     elif choice == "1": >     for critter in farmlet: >     farmlet.talk() You want to call .talk() on your "critter" instance which has the .talk() method, not on farmlet (which is a list as the error m

[Tutor] Class methods

2011-06-22 Thread David Merrick
Can someone show me how to code this correctly please? # Critter Caretaker # A virtual pet to care for class Critter(object): """A virtual pet""" def __init__(self, name, hunger = 0, boredom = 0): self.name = name self.hunger = hunger self.boredom = boredom #

Re: [Tutor] Class methods

2011-06-22 Thread Alan Gauld
"David Merrick" wrote class Critter(object): def __init__(self, name, hunger = 0, boredom = 0): def __pass_time(self): def __str__(self): @property def mood(self): def talk(self): def eat(self): def play(self): class Farm(Critter): I still don't think a Farm is a typ

[Tutor] Class methods

2011-06-21 Thread David Merrick
# Critter Caretaker # A virtual pet to care for class Critter(object): """A virtual pet""" def __init__(self, name, hunger = 0, boredom = 0): self.name = name self.hunger = hunger self.boredom = boredom # __ denotes private method def __pass_time(self):

Re: [Tutor] class methods as static methods?

2010-05-30 Thread Alex Hall
On 5/30/10, Alan Gauld wrote: > "Alex Hall" wrote > >> that it will hit. I would like to not instantiate a Harpoon object, >> just call the Harpoon's getImpactCoords method and pass it the >> required arguments. Is this possible? > > Others have pointed out that > a) This is possible using static

Re: [Tutor] class methods as static methods?

2010-05-30 Thread Alan Gauld
"Alex Hall" wrote that it will hit. I would like to not instantiate a Harpoon object, just call the Harpoon's getImpactCoords method and pass it the required arguments. Is this possible? Others have pointed out that a) This is possible using staticmetjhod or classmetjod decorators and b) it s

Re: [Tutor] class methods as static methods?

2010-05-29 Thread Mark Lawrence
Hi Alex, thanks for the response, please see below. On 30/05/2010 02:50, Alex Hall wrote: On 5/29/10, Mark Lawrence wrote: On 29/05/2010 20:49, Alex Hall wrote: Hi all, In Battleship, I have a weapons.py file, currently with just one missile type (a Harpoon anti-ship missile). This Harpoon cl

Re: [Tutor] class methods as static methods?

2010-05-29 Thread Steven D'Aprano
On Sun, 30 May 2010 05:49:45 am Alex Hall wrote: > Hi all, > In Battleship, I have a weapons.py file, currently with just one > missile type (a Harpoon anti-ship missile). This Harpoon class > defines a getImpactCoords method, which returns all coordinates on > the map that it will hit. I would lik

Re: [Tutor] class methods as static methods?

2010-05-29 Thread Alex Hall
On 5/29/10, Mark Lawrence wrote: > On 29/05/2010 20:49, Alex Hall wrote: >> Hi all, >> In Battleship, I have a weapons.py file, currently with just one >> missile type (a Harpoon anti-ship missile). This Harpoon class defines >> a getImpactCoords method, which returns all coordinates on the map >>

Re: [Tutor] class methods as static methods?

2010-05-29 Thread Mark Lawrence
On 29/05/2010 20:49, Alex Hall wrote: Hi all, In Battleship, I have a weapons.py file, currently with just one missile type (a Harpoon anti-ship missile). This Harpoon class defines a getImpactCoords method, which returns all coordinates on the map that it will hit. I would like to not instantiat

Re: [Tutor] class methods as static methods?

2010-05-29 Thread Lie Ryan
On 05/30/10 05:49, Alex Hall wrote: > Hi all, > In Battleship, I have a weapons.py file, currently with just one > missile type (a Harpoon anti-ship missile). This Harpoon class defines > a getImpactCoords method, which returns all coordinates on the map > that it will hit. I would like to not inst

[Tutor] class methods as static methods?

2010-05-29 Thread Alex Hall
Hi all, In Battleship, I have a weapons.py file, currently with just one missile type (a Harpoon anti-ship missile). This Harpoon class defines a getImpactCoords method, which returns all coordinates on the map that it will hit. I would like to not instantiate a Harpoon object, just call the Harpoo

Re: [Tutor] class methods: using class vars as args?

2010-05-28 Thread Steven D'Aprano
On Fri, 28 May 2010 07:42:30 am Alex Hall wrote: > Thanks for all the explanations, everyone. This does make sense, and > I am now using the > if(arg==None): arg=self.arg > idea. It only adds a couple lines, and is, if anything, more explicit > than what I was doing before. You should use "if arg

Re: [Tutor] class methods: using class vars as args?

2010-05-27 Thread Alex Hall
Thanks for all the explanations, everyone. This does make sense, and I am now using the if(arg==None): arg=self.arg idea. It only adds a couple lines, and is, if anything, more explicit than what I was doing before. On 5/27/10, Mark Lawrence wrote: > On 23/05/2010 20:40, Alex Hall wrote: >> Hello

Re: [Tutor] class methods: using class vars as args?

2010-05-27 Thread Mark Lawrence
On 23/05/2010 20:40, Alex Hall wrote: Hello all, I know Python reasonably well, but I still run into basic questions which those over on the other python list request I post here instead. I figure this would be one of them: Why would this not work: class c(object): def __init__(self, arg1, arg

Re: [Tutor] class methods: using class vars as args?

2010-05-27 Thread spir ☣
On Sun, 23 May 2010 15:40:13 -0400 Alex Hall wrote: > Hello all, > I know Python reasonably well, but I still run into basic questions > which those over on the other python list request I post here instead. > I figure this would be one of them: > Why would this not work: > > class c(object): >

Re: [Tutor] class methods: using class vars as args?

2010-05-23 Thread Matthew Wood
Hey Alex, What's happening is that you're still in "defining functions" mode on the line def doSomething(self, arg3=self.arg1): self, which is really nothing more than a parameter being passed in (special parameter, but a parameter none the less) hasn't been assigned a value yet. Imagine this f

Re: [Tutor] class methods: using class vars as args?

2010-05-23 Thread Alan Gauld
"Alex Hall" wrote class c(object): def __init__(self, arg1, arg2): self.arg1=arg1 self.arg2=arg2 def doSomething(self, arg3=self.arg1): ... The above results in an error that "name 'self' is not defined". Why can I not set the default values of a method's arguments to class vars like th

[Tutor] class methods: using class vars as args?

2010-05-23 Thread Alex Hall
Hello all, I know Python reasonably well, but I still run into basic questions which those over on the other python list request I post here instead. I figure this would be one of them: Why would this not work: class c(object): def __init__(self, arg1, arg2): self.arg1=arg1 self.arg2=arg2 d

Re: [Tutor] class methods as argument

2007-02-10 Thread Kent Johnson
thomas coopman wrote: > On Sat, 10 Feb 2007 09:04:15 -0500 > Kent Johnson <[EMAIL PROTECTED]> wrote: >> If you do want to keep FooList then you should call >> SortedList.__init__() to set the compare function. >> SortedList.__init__ is an unbound function so you call it like this: >>SortedLis

Re: [Tutor] class methods as argument

2007-02-10 Thread thomas coopman
On Sat, 10 Feb 2007 09:04:15 -0500 Kent Johnson <[EMAIL PROTECTED]> wrote: > thomas coopman wrote: > > > > also, > > Is it better to use super in FooList? and how should I use it then? > > Actually I would say that FooList is not pulling its weight. > SortedList already allows specialization by

Re: [Tutor] class methods as argument

2007-02-10 Thread Kent Johnson
thomas coopman wrote: > Thank you for the explanation of bound and unbound methods. > I understand that it is more common to use a bound method, but I don't > think that I can use this because at the time I save the method, I don't > know anything about the instances. That would be an appropriate

Re: [Tutor] class methods as argument

2007-02-10 Thread thomas coopman
On Sat, 10 Feb 2007 07:55:54 -0500 Kent Johnson <[EMAIL PROTECTED]> wrote: > thomas coopman wrote: > > Hi, > > > > I want to do something like this, don't know how to properly > > explain it, so I just give you some example code > > > class Foo(object): > def method(self, arg): >

Re: [Tutor] class methods as argument

2007-02-10 Thread Andrei
Hi Thomas, thomas coopman wrote: > I want to execute the class method given as argument, > but this obvious doesn't work, but I don't know how to > get it work, Pass the method reference of the class as argument, like this: >>> class A(object): ... def __init__(self, x): self.x = x ...

Re: [Tutor] class methods as argument

2007-02-10 Thread Kent Johnson
thomas coopman wrote: > Hi, > > I want to do something like this, don't know how to properly explain it, > so I just give you some example code > class Foo(object): def method(self, arg): print arg > def doSomething(object, func): object.func("test") >

Re: [Tutor] class methods as argument

2007-02-10 Thread thomas coopman
On Sat, 10 Feb 2007 22:10:52 +1000 Jonathan McManus <[EMAIL PROTECTED]> wrote: > It's pretty easy to make this work, actually. The issue is in the > "doSomething" method. > > > >>>class Foo(object): > > >>> def method(self, arg): > > >>> print arg > > > > >>>def doSomething(object, func)

[Tutor] class methods as argument

2007-02-10 Thread thomas coopman
Hi, I want to do something like this, don't know how to properly explain it, so I just give you some example code >>>class Foo(object): >>> def method(self, arg): >>> print arg >>>def doSomething(object, func): >>> object.func("test") >>>object = Foo() >>>doSomething(object,