473,394 Members | 1,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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
13 3020
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Gerard Flanagan | last post by:
Hello all I would like to do the following: from elementtree.SimpleXMLWriter import XMLWriter class HtmlWriter(XMLWriter, object): def write_raw(self, text): super( HtmlWriter, self...
3
by: mrdylan | last post by:
Given a class like so: ------------------------------- class TestMe(object): def get(self): pass def set(self, v): pass p = property( get, set )
4
by: rzed | last post by:
I'm confused (not for the first time). I create these classes: class T(object): def __new__(self): self.a = 1 class X(T): def __init__(self):
5
by: furqi | last post by:
hi every body i write a code in c sharp in which i have made a base class and make an event there.Then i make a derived class and made an other event in that class. Now what i wanna do is that i...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.