Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #7258 > unrolled thread

Re: Of Functions, Objects, and Methods-I NEED HELP PLEASE

Started byBenjamin Kaplan <benjamin.kaplan@case.edu>
First post2011-06-08 13:34 -0700
Last post2011-06-08 13:34 -0700
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Of Functions, Objects, and Methods-I NEED HELP PLEASE Benjamin Kaplan <benjamin.kaplan@case.edu> - 2011-06-08 13:34 -0700

#7258 — Re: Of Functions, Objects, and Methods-I NEED HELP PLEASE

FromBenjamin Kaplan <benjamin.kaplan@case.edu>
Date2011-06-08 13:34 -0700
SubjectRe: Of Functions, Objects, and Methods-I NEED HELP PLEASE
Message-ID<mailman.34.1307565299.11593.python-list@python.org>
On Wed, Jun 8, 2011 at 1:09 PM, Cathy James <nambo4jb@gmail.com> wrote:
> I am almost there, but I need a little help:
>
> I would like to
>
> a) print my dogs in the format  index. name: breed as follows:
>
> 0. Mimi:Poodle
> 1.Sunny: Beagle
> 2. Bunny: German Shepard
> I am getting
>
> (0, ('Mimi', 'Poodle')) . Mimi : Poodle instead-what have I done wrong?
>
> b) I would like to append to my list, but my line dogs.dogAppend() is
> giving a TypeError:
>
> for i in enumerate (self.dogAppend()):
> TypeError: 'list' object is not callable
>
> Any help?
>
> #MY CODE BELOW:
>
> import sys
> class Dog():
>    def __init__(self, name, breed):
>        self.name = name
>        self.breed = breed
>
>    def dogAppend(self):
>        self.dogAppend = []
>        self.dogAppend.append((self.name,self.breed))
>        return self.dogAppend
>

In Python, everything is an object. And when we say everything, we
really do mean everything. A function is an object too. So when you do
self.dogAppend = [], you're replacing self.dogAppend (the function
object) with a list.

>
>    def display (self):
>        for i in enumerate (self.dogAppend()):

I don't know what you're trying to do here, because this makes no
sense. dogAppend is going to return a list of a single object, so it's
going to be 0 every time. And enumerate returns two things: the index
and the object. Since you only specified one variable, you get the
tuple of (index, object) which is what you're seeing.

You're supposed to do :

    for i, dog in enumerate(a_list_of_all_the_dogs) :
            print (i,".",  dog.name, ": " + dog.breed)

for what you're  trying to get.

>
> if __name__ == "__main__":
>    dogs = Dog(name=input (" Enter Dog Name: "), breed=input ("Enter
> Dog Breed: "))
>    while not dogs:
>        print("Goodbye!!")
>        sys.exit()
>    else:
>        #dogs.dogAppend()
>        dogs.display()
> --
> http://mail.python.org/mailman/listinfo/python-list
>

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web