467,135 Members | 1,123 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,135 developers. It's quick & easy.

class problem, NoneType obj has no attribute

wassup here?

>>>
7

Traceback (most recent call last):
File "C:\Python25\myPrograms\netflix\netflix.py", line 22, in
<module>
print cust1.getID()
AttributeError: 'NoneType' object has no attribute 'getID'
>>>


class customer:
def __init__(self, ID, movies):
self.ID = ID
self.movies = movies

def getID():
return self.ID

def getMovies():
return self.movies
import os
import customer
import movie

mv1 = open('C:\\Python25\\myPrograms\\netflix\\mv1exp2.t xt', 'r+')
mv1str = mv1.read()

print mv1str.count(',5,')

cust1 = customer.__init__('12',['1','435','2332'])
print cust1.getID()
Jun 27 '08 #1
  • viewed: 2664
Share:
13 Replies
globalrev schrieb:
cust1 = customer.__init__('12',['1','435','2332'])
cust1 = customer('12',['1','435','2332'])

Christian

Jun 27 '08 #2
Christian Heimes wrote:
globalrev schrieb:
>cust1 = customer.__init__('12',['1','435','2332'])

cust1 = customer('12',['1','435','2332'])
.... and before that

from custumer import customer

Peter
Jun 27 '08 #3
Christian Heimes wrote:
globalrev schrieb:
>cust1 = customer.__init__('12',['1','435','2332'])

cust1 = customer('12',['1','435','2332'])
.... and before that

from customer import customer

Peter
Jun 27 '08 #4
globalrev a écrit :
wassup here?
7

Traceback (most recent call last):
File "C:\Python25\myPrograms\netflix\netflix.py", line 22, in
<module>
print cust1.getID()
AttributeError: 'NoneType' object has no attribute 'getID'

class customer:
1/ naming convention : class names should be CamelCased
2/ unless you need compatibility with years old Python versions, use
newstyle classes

class Customer(object):
def __init__(self, ID, movies):
self.ID = ID
self.movies = movies
3/ naming conventions : ALL_UPPER_NAMES denote (pseudo) constants

def __init__(self, id, movies):
self.id = id
self.movies = movies
def getID():
return self.ID

def getMovies():
return self.movies
4/ Python has support for computed attributes, so you just don't need
these getters.
>
import os
import customer
import movie

mv1 = open('C:\\Python25\\myPrograms\\netflix\\mv1exp2.t xt', 'r+')
mv1str = mv1.read()

print mv1str.count(',5,')

cust1 = customer.__init__('12',['1','435','2332'])
__init__ is automagically called on instanciation, so you don't have to
call it yourself. And FWIW, your __init__ returns None.

cust1 = Customer('12', ['1', '435', '2332'])
print cust1.getID()
print cust1.id

Jun 27 '08 #5
On 16 Maj, 13:54, Peter Otten <__pete...@web.dewrote:
Christian Heimes wrote:
globalrev schrieb:
cust1 = customer.__init__('12',['1','435','2332'])
cust1 = customer('12',['1','435','2332'])

... and before that

from customer import customer

Peter
why do i have to write that?

if i do import customer im importing everything no?

but youre right it doesnt work unless i do, i just dont get why.
Jun 27 '08 #6
On 16 Maj, 14:19, Bruno Desthuilliers <bruno.
42.desthuilli...@websiteburo.invalidwrote:
globalrev a écrit :
wassup here?
7
Traceback (most recent call last):
File "C:\Python25\myPrograms\netflix\netflix.py", line 22, in
<module>
print cust1.getID()
AttributeError: 'NoneType' object has no attribute 'getID'
class customer:

1/ naming convention : class names should be CamelCased
2/ unless you need compatibility with years old Python versions, use
newstyle classes

class Customer(object):
def __init__(self, ID, movies):
self.ID = ID
self.movies = movies

3/ naming conventions : ALL_UPPER_NAMES denote (pseudo) constants

def __init__(self, id, movies):
self.id = id
self.movies = movies
def getID():
return self.ID
def getMovies():
return self.movies

4/ Python has support for computed attributes, so you just don't need
these getters.
import os
import customer
import movie
mv1 = open('C:\\Python25\\myPrograms\\netflix\\mv1exp2.t xt', 'r+')
mv1str = mv1.read()
print mv1str.count(',5,')
cust1 = customer.__init__('12',['1','435','2332'])

__init__ is automagically called on instanciation, so you don't have to
call it yourself. And FWIW, your __init__ returns None.

cust1 = Customer('12', ['1', '435', '2332'])
print cust1.getID()

print cust1.id

what should init return normally? a customer-object?

class customer:
def __init__(self, idnbr, movies):
self.idnbr = idnbr
self.movies = movies
return self
?
Jun 27 '08 #7
On 16 Maj, 14:19, Bruno Desthuilliers <bruno.
42.desthuilli...@websiteburo.invalidwrote:
globalrev a écrit :
wassup here?
7
Traceback (most recent call last):
File "C:\Python25\myPrograms\netflix\netflix.py", line 22, in
<module>
print cust1.getID()
AttributeError: 'NoneType' object has no attribute 'getID'
class customer:

1/ naming convention : class names should be CamelCased
2/ unless you need compatibility with years old Python versions, use
newstyle classes

class Customer(object):
def __init__(self, ID, movies):
self.ID = ID
self.movies = movies

3/ naming conventions : ALL_UPPER_NAMES denote (pseudo) constants

def __init__(self, id, movies):
self.id = id
self.movies = movies
def getID():
return self.ID
def getMovies():
return self.movies

4/ Python has support for computed attributes, so you just don't need
these getters.
import os
import customer
import movie
mv1 = open('C:\\Python25\\myPrograms\\netflix\\mv1exp2.t xt', 'r+')
mv1str = mv1.read()
print mv1str.count(',5,')
cust1 = customer.__init__('12',['1','435','2332'])

__init__ is automagically called on instanciation, so you don't have to
call it yourself. And FWIW, your __init__ returns None.

cust1 = Customer('12', ['1', '435', '2332'])
print cust1.getID()

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

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

when class =
class customer:
def __init__(self, idnbr, movies):
self.idnbr = idnbr
self.movies = movies

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

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

when class =
class customer:
def __init__(self, idnbr, movies):
self.idnbr = idnbr
self.movies = movies

Jun 27 '08 #8
globalrev wrote:
On 16 Maj, 13:54, Peter Otten <__pete...@web.dewrote:
>Christian Heimes wrote:
globalrev schrieb:
cust1 = customer.__init__('12',['1','435','2332'])
cust1 = customer('12',['1','435','2332'])

... and before that

from customer import customer

Peter

why do i have to write that?

if i do import customer im importing everything no?

but youre right it doesnt work unless i do, i just dont get why.
It becomes clearer if you follow the usual naming conventions and start the
class name with an uppercase letter:
# in file customer.py

class Customer:
# your code

The import then becomes

from customer import Customer

i. e. the first "customer" denotes the module, the second "Customer" the
class.

Peter

Jun 27 '08 #9
On Fri, 2008-05-16 at 06:07 -0700, globalrev wrote:
On 16 Maj, 14:19, Bruno Desthuilliers <bruno.
42.desthuilli...@websiteburo.invalidwrote:
globalrev a écrit :
wassup here?
7
Traceback (most recent call last):
File "C:\Python25\myPrograms\netflix\netflix.py", line 22, in
<module>
print cust1.getID()
AttributeError: 'NoneType' object has no attribute 'getID'
class customer:
1/ naming convention : class names should be CamelCased
2/ unless you need compatibility with years old Python versions, use
newstyle classes

class Customer(object):
def __init__(self, ID, movies):
self.ID = ID
self.movies = movies
3/ naming conventions : ALL_UPPER_NAMES denote (pseudo) constants

def __init__(self, id, movies):
self.id = id
self.movies = movies
def getID():
return self.ID
def getMovies():
return self.movies
4/ Python has support for computed attributes, so you just don't need
these getters.
import os
import customer
import movie
mv1 = open('C:\\Python25\\myPrograms\\netflix\\mv1exp2.t xt', 'r+')
mv1str = mv1.read()
print mv1str.count(',5,')
cust1 = customer.__init__('12',['1','435','2332'])
__init__ is automagically called on instanciation, so you don't have to
call it yourself. And FWIW, your __init__ returns None.

cust1 = Customer('12', ['1', '435', '2332'])
print cust1.getID()
print cust1.id


what should init return normally? a customer-object?

class customer:
def __init__(self, idnbr, movies):
self.idnbr = idnbr
self.movies = movies
return self
?
No. __init__ should not return anything (rather, it should return None,
but it'll do that on its own without you telling it to return anything).
The problem is that you are trying to create an object with __init__,
but that is not what it does. It *initializes* objects that already
exist. when you call a class, it creates a new instance using
__new__(), and then initializes it using __init__(). So if you were
really inclined to muck around. You could do something like

cust1 = Customer.__new__(Customer)
cust1.__init__('12', ['1','435', '2332'])

But that would just be silly. What you really want is this:

cust1 = Customer('12', ['1','435', '2332'])

which does the same thing. Don't explicitly call under-under variables
unless you really have to.

Cheers,
Cliff

Jun 27 '08 #10
globalrev a écrit :
On 16 Maj, 14:19, Bruno Desthuilliers <bruno.
42.desthuilli...@websiteburo.invalidwrote:
>globalrev a écrit :
(snip)
>>cust1 = customer.__init__('12',['1','435','2332'])
__init__ is automagically called on instanciation, so you don't have to
call it yourself. And FWIW, your __init__ returns None.

what should init return normally?
None. Your __init__ is fine, the problem is with the call. To
instanciate a class, you call the class object itself, passing it the
arguments awaited by __init__, ie:
cust1 = Customer('12', ['1', '435', '2332'])
Jun 27 '08 #11
globalrev wrote:
On 16 Maj, 14:19, Bruno Desthuilliers <bruno.
42.desthuilli...@websiteburo.invalidwrote:
>globalrev a écrit :
>>wassup here?
7
Traceback (most recent call last):
File "C:\Python25\myPrograms\netflix\netflix.py", line 22, in
<module>
print cust1.getID()
AttributeError: 'NoneType' object has no attribute 'getID'
class customer:
1/ naming convention : class names should be CamelCased
2/ unless you need compatibility with years old Python versions, use
newstyle classes

class Customer(object):
>> def __init__(self, ID, movies):
self.ID = ID
self.movies = movies
3/ naming conventions : ALL_UPPER_NAMES denote (pseudo) constants

def __init__(self, id, movies):
self.id = id
self.movies = movies
>> def getID():
return self.ID
def getMovies():
return self.movies
4/ Python has support for computed attributes, so you just don't need
these getters.
>>import os
import customer
import movie
mv1 = open('C:\\Python25\\myPrograms\\netflix\\mv1exp2.t xt', 'r+')
mv1str = mv1.read()
print mv1str.count(',5,')
cust1 = customer.__init__('12',['1','435','2332'])
__init__ is automagically called on instanciation, so you don't have to
call it yourself. And FWIW, your __init__ returns None.

cust1 = Customer('12', ['1', '435', '2332'])
>>print cust1.getID()
print cust1.id

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

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

when class =
class customer:
def __init__(self, idnbr, movies):
self.idnbr = idnbr
self.movies = movies

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

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

when class =
class customer:
def __init__(self, idnbr, movies):
self.idnbr = idnbr
self.movies = movies
You really should back up and go through the Python tutorial.

cust1.movies() tells Python to call the object pointed to by cust1.movies (note
the parenthesis after .movies). Since that object (in your case) is a list,
that makes no sense (which is exactly what the traceback said).

print "cust", cust1.idnbr, cust1.movies

is what you want here.

-Larry
Jun 27 '08 #12
globalrev a écrit :
On 16 Maj, 14:19, Bruno Desthuilliers <bruno.
42.desthuilli...@websiteburo.invalidwrote:
>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
Jun 27 '08 #13
On Fri, 2008-05-16 at 06:04 -0700, globalrev wrote:
On 16 Maj, 13:54, Peter Otten <__pete...@web.dewrote:
Christian Heimes wrote:
globalrev schrieb:
>cust1 = customer.__init__('12',['1','435','2332'])
cust1 = customer('12',['1','435','2332'])
... and before that

from customer import customer

Peter

why do i have to write that?

if i do import customer im importing everything no?
Not exactly. That's how perl and PHP tend to do it, but python's import
system is much more conservative (and far superior because of it). When
you import a name you get that name and nothing else. You can still
call your class without doing from customer import customer, but you
have to include the module in the class name like this:

cust1 = customer.customer('12',['1','435','2332'])

Everything stays hidden behind the name customer. That way, if customer
defines a class that has the same name that you already have in your
namespace, it won't get overwritten. Say you define a class Purchase in
your customer module, and then in the interpreter, you write

py>>import customer
py>>Purchase = 4
py>>bread = customer.Purchase('bread')
py>>Purchase
4
py>>bread
<customer.Purchase object at 0xabcdefab>
py>>>

Purchase and customer.Purchase can live happily side by side. In perl
or PHP, the default behavior causes errors which are hard to debug
because you can't see what names are being pulled into your namespace.
but youre right it doesnt work unless i do, i just dont get why.
--
http://mail.python.org/mailman/listinfo/python-list
Jun 27 '08 #14

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Gerard Flanagan | last post: by
4 posts views Thread by rzed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.