Attribute Error

This is the place for queries that don't fit in any of the other categories.

Attribute Error

Postby amthompson4 » Fri Jul 12, 2013 9:35 pm

This might be an elementary error, but I do not know why the output is not showing. I have been trying to learn some python by myself, and I just need some help with whats wrong with this. I am using python 2.7.5
Code: Select all
>>> class FooClass(object):
   """my very first class: FooClass"""
   version = 0.1
   def _init_(self, r='John Doe'):
      """constructor"""
      self.name = r    #class instance (data) attribute
      print 'Created a class instance for', r
   def showname(self):
      """display instance attribute and class name"""
      print 'Your name is', self.name
      print 'My name is', self._class_._name_
   def showver(self):
      """display class(static) attribute"""
      print self.version  #references FooClass.version
   def addMe2Me(self,x):
      """apply + operation to arguement"""
      return x + x

   
>>> fool=FooClass()
>>> fool.showname()
Your name is

Traceback (most recent call last):
  File "<pyshell#64>", line 1, in <module>
    fool.showname()
  File "<pyshell#62>", line 10, in showname
    print 'Your name is', self.name
AttributeError: 'FooClass' object has no attribute 'name'
amthompson4
 
Posts: 3
Joined: Fri Jul 12, 2013 9:31 pm

Re: Attribute Error

Postby Yoriz » Fri Jul 12, 2013 9:45 pm

The errors are on lines that should have double before and after underscores.

The line
Code: Select all
def _init_(self, r='John Doe'):
Should be
Code: Select all
def __init__(self, r='John Doe'):


Also
Code: Select all
print 'My name is', self._class_._name_
Should be
Code: Select all
print 'My name is', self.__class__.__name__
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 777
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Attribute Error

Postby metulburr » Fri Jul 12, 2013 9:46 pm

it's __init__ not _init_, note: two _ _ on each side


Why are you using the interpreter to write the class?
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1383
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Attribute Error

Postby amthompson4 » Fri Jul 12, 2013 9:54 pm

Im just starting out, so I do not know too much about python. Is there somewhere else I am suppose to be creating the class at?
amthompson4
 
Posts: 3
Joined: Fri Jul 12, 2013 9:31 pm

Re: Attribute Error

Postby Yoriz » Fri Jul 12, 2013 10:04 pm

It would be easier to use an ide so your code is saved to a file which you can run again and edit
There is a bit of a trutorial here see the ide part.
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 777
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Attribute Error

Postby amthompson4 » Fri Jul 12, 2013 10:07 pm

Oh okay, I'm using IDLE just not the IDE part of IDLE I guess you could say. That makes more sense thought to do it there so I can save it, and go back to it later. Thanks guys.
amthompson4
 
Posts: 3
Joined: Fri Jul 12, 2013 9:31 pm


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 1 guest