class problem, NoneType obj has no attribute

Bruno Desthuilliers bruno.42.desthuilliers at websiteburo.invalid
Fri May 16 10:11:36 EDT 2008


globalrev a écrit :
> On 16 Maj, 14:19, Bruno Desthuilliers <bruno.
> 42.desthuilli... at websiteburo.invalid> wrote:
>> globalrev a écrit :
(snip)
>>>     def getMovies():
>>>         return self.movies
>> 4/ Python has support for computed attributes, so you just don't need
>> these getters.

 > when class =
 > class customer:

Please, do yourself and the world a favour, follow naming conventions 
(not only will this make your code more readable, but also quite a lot 
of things in Python rely on conventions).

And really, use newstyle classes unless you have a *very* compelling 
reason not to.

class Customer(object):

 >     def __init__(self, idnbr, movies):
 >         self.idnbr = idnbr
 >         self.movies = movies
 >

> print "cust", cust1.idnbr, cust1.movies()

Why do you want to call a list ???

> Traceback (most recent call last):
>   File "C:\Python25\myPrograms\netflix\netflix.py", line 24, in
> <module>
>     print "cust", cust1.idnbr, cust1.movies()
> TypeError: 'list' object is not callable

Indeed. cust1.movie being a list, that's just what you would expect. 
Hint : what do you think will happen with the following code:

   alist = [1, 2, 3]
   alist()

When I say that Python has support for computed attributes, it doesn't 
mean it will automagically add getters/setters for your attributes, but 
that by default you just *don't need* getters/setters - you'll be able 
to transparently add them latter if there's an effective need for them - 
where "transparently" means "without client code knowing it's in fact 
calling a getter or setter".

Example:

# before:
class Foo(object):
    def __init__(self, name):
       # just a plain attribute assignment
       self.name = name

f = Foo('bruno')
# just a plain attribute access
print f.name

# after
class Foo(object):
    def __init__(self, name):
       # still looks like a plain attribute assignment
       self.name = name

    # defining the setter and getter
    def _setname(self, name):
      print "setting %s name to '%s'" % (self, name)
      self._name = name
    def _getname(self):
      print "retrieving %s name ('%s')" % (self, self._name)
      return self._name.upper()

    # and making this a computed attribute using the getter and
    # setter under the hood
    name = property(fget=_getname, fset=_setname)

f = Foo('bruno')
# still looks like a plain attribute access
print f.name

Also, given your recent posts here, I strongly suggest you take some 
time doing at least the official tutorial.

HTH



More information about the Python-list mailing list