Some basic newbie questions...
Hi all, While working on support at work, I have been picking away at Python - because I think it could be a valuable scripting tool for building utilities from. I have been reading the python.org tutorials, and playing around with some basic code, but I have ended up with a few questions that probably have straightforward answers - any quick explanations or assistance would be fantastic... Question 1... Given the code below, why does the count method return what it does? How *should* you call the count method? a = [] a.append(1) print a.count Question 2... What is the correct way of looping through a list object in a class via a method of it? (I've hit all sorts of errors picking away at this, and none of the tutorials I've found so far cover it very well) - apologies for the arbitrary class - it's the first example I thought up... class Gun: Shells = 10 class Battleship: Gun1 = Gun() Gun2 = Gun() Guns = [Gun1,Gun2] def getShellsLeft(self): NumShells = 0 for aGun in Guns: NumShells = NumShells + aGun.Shells return NumShells Bizmark = Battleship() print Bizmark.getShellsLeft() In the above code, I guess I'm just asking for the *correct* way to do these simple kinds of things... -- http://mail.python.org/mailman/listinfo/python-list
Re: Some basic newbie questions...
Chris Mellon wrote: > On 28 Dec 2006 08:40:02 -0800, jonathan.beckett > <[EMAIL PROTECTED]> wrote: > > Hi all, > > > > While working on support at work, I have been picking away at Python - > > because I think it could be a valuable scripting tool for building > > utilities from. I have been reading the python.org tutorials, and > > playing around with some basic code, but I have ended up with a few > > questions that probably have straightforward answers - any quick > > explanations or assistance would be fantastic... > > > > > > Question 1... > > Given the code below, why does the count method return what it does? > > How *should* you call the count method? > > a = [] > > a.append(1) > > print a.count > > > > print a.count(). There's a "Python for VB programmers" out there > somewhere, see if you can find it. In python, functions (and methods > which are special cases of functions) are first class objects, so > you're printing the function object, not calling it. > > > > > Question 2... > > What is the correct way of looping through a list object in a class via > > a method of it? (I've hit all sorts of errors picking away at this, and > > none of the tutorials I've found so far cover it very well) - apologies > > for the arbitrary class - it's the first example I thought up... > > > > class Gun: > > Shells = 10 > > > > class Battleship: > > Gun1 = Gun() > > Gun2 = Gun() > > Guns = [Gun1,Gun2] > > > > def getShellsLeft(self): > > NumShells = 0 > > for aGun in Guns: > > NumShells = NumShells + aGun.Shells > > return NumShells > > > > Bizmark = Battleship() > > > > print Bizmark.getShellsLeft() > > > > > > In the above code, I guess I'm just asking for the *correct* way to do > > these simple kinds of things... > > > > You have the right idea but you've got your object instantiation > wrong. As I write this, I see an email from Grant Edwards that sums > things up nicely, I'll let him explain it. > > I suggest working through the Python tutorial and Dive Into Python, > which will introduce you to the concepts you're getting wrong here. > > > -- > > http://mail.python.org/mailman/listinfo/python-list > > I'm not a VB programmer :) I'm just tooling around with Python while I have time, figuring out how the syntax hangs together before I start trying to do anything remotely clever with it. I normally work with PHP, C#, Javascript, and the odd bit of C++, but find myself doing christmas support, so am looking for something to pick away at until the phone rings :) One thing I would like to do is play with pySQLite, which looks like it might be very handy indeed for making migration tools... -- http://mail.python.org/mailman/listinfo/python-list
Re: Some basic newbie questions...
Hi Grant, thanks for the code snippets - made total sense. On the evidence of the last couple of hours, Python is still feeling very alien, but I'm starting to get my head around it. I'm just finding it a bit weird that some of the built in functions are static, rather than methods of objects (such as len() being used to find the length of a list). Anyway - cheers again! Jonathan -- http://mail.python.org/mailman/listinfo/python-list
Re: Some basic newbie questions...
> > I normally work with PHP, C#, Javascript, and the odd bit of C++, > > Do any of them call functions w/o parens? That's a really good point :) -- http://mail.python.org/mailman/listinfo/python-list
Re: Some basic newbie questions...
> Too many misconceptions here (I changed to a more PEP-8 style naming): > >class Gun(object): >def __init__(self): >self.shells = 10 > >class Battleship(object): >def __init__(self): >self.guns = [Gun(), Gun()] > >def getShellsLeft(self): >numShells = 0 >for aGun in self.guns: >numShells += aGun.shells >return numShells > >theBizmark = Battleship() >print theBizmark.getShellsLeft() Excellent example - once upon a time I used to write very neat code indeed, but exposure to C# had pretty much knackered that (where all the framework object methods are capitalized). -- http://mail.python.org/mailman/listinfo/python-list
