Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > defining classes

Reply
Thread Tools

defining classes

 
 
LeRoy Lee
Guest
Posts: n/a
 
      09-02-2005
I have been searching for the answer to this as it will determine how I use
classes. Here are two bits of code.

class foo1:
def __init__(self, i):
self.r = i
self.j = 5

>>h = foo1(1)
>>h.r

1
>>h.j

5


Now take this example

class foo2:
def __init__(self):
self.j = 5

>>h = foo2()
>>h.j

Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: foo2 instance has no attribute 'j'

I can't figure out why it is working this way. I figure I must be thinking
about this wrong. I was thinking that I could bind attributes to the class
from within methods using the self prefix. According to this example I can
only when passing other info into the init. Is there a rule that I am just
not aware off? Am I totally off base (I am not real experienced)? What is
the self prefix for then if not to bind up the tree?

Thanks,
LeRoy

__________________________________________________ _______________
Express yourself instantly with MSN Messenger! Download today - it's FREE!
http://messenger.msn.click-url.com/g...ave/direct/01/

 
Reply With Quote
 
 
 
 
Michael Hoffman
Guest
Posts: n/a
 
      09-02-2005
LeRoy Lee wrote:

> class foo2:
> def __init__(self):
> self.j = 5
>
>>> h = foo2()
>>> h.j

>
> Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> AttributeError: foo2 instance has no attribute 'j'


Try again:

>>> class foo2:

.... def __init__(self):
.... self.j = 5
....
>>> h = foo2()
>>> h.j

5
--
Michael Hoffman
 
Reply With Quote
 
 
 
 
Grant Edwards
Guest
Posts: n/a
 
      09-02-2005
On 2005-09-02, LeRoy Lee <(E-Mail Removed)> wrote:

> Now take this example
>
> class foo2:
> def __init__(self):
> self.j = 5
>
>>>h = foo2()
>>>h.j

> Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> AttributeError: foo2 instance has no attribute 'j'


Works fine for me either "batch" mode:

$ cat testit.py
class foo2:
def __init__(self):
self.j = 5

h = foo2()
print h.j

$ python testit.py
5

or interactivly:

Python 2.3.4 (#2, Aug 25 2005, 10:06:55)
[GCC 3.4.1 (Mandrakelinux 10.1 3.4.1-4mdk)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> class foo2:

... def __init__(self):
... self.j = 5
...
>>> h = foo2()
>>> h.j

5
>>>


--
Grant Edwards grante Yow! I'm definitely not
at in Omaha!
visi.com
 
Reply With Quote
 
Steve Horsley
Guest
Posts: n/a
 
      09-02-2005
LeRoy Lee wrote:
> I have been searching for the answer to this as it will determine how I
> use classes. Here are two bits of code.
>
> class foo1:
> def __init__(self, i):
> self.r = i
> self.j = 5
>
>>> h = foo1(1)
>>> h.r

>
> 1
>
>>> h.j

>
> 5
>
>
> Now take this example
>
> class foo2:
> def __init__(self):
> self.j = 5
>
>>> h = foo2()
>>> h.j

>
> Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> AttributeError: foo2 instance has no attribute 'j'
>
> I can't figure out why it is working this way. I figure I must be
> thinking about this wrong. I was thinking that I could bind attributes
> to the class from within methods using the self prefix. According to
> this example I can only when passing other info into the init. Is there
> a rule that I am just not aware off? Am I totally off base (I am not
> real experienced)? What is the self prefix for then if not to bind up
> the tree?
>


It works for me.

>>> class foo2:

.... def __init__(self):
.... self.j = 5
....
>>> h = foo2()
>>> h.j

5
>>>


Are you sure you clicked the save button of the editor before
running the code? (Been there, done that myself.)

Or if you're importing a module that contains the code, did you
reload the module after editing the code and before creating a
new class instance? (Been there, wasted lots of time myself.)


Steve

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Defining many classes in a single file Aguilar, James C++ 9 07-14-2004 08:29 PM
defining or not defining destructors johny smith C++ 8 07-02-2004 08:51 AM
Defining constants in classes Joe C++ 29 03-04-2004 03:03 PM
CSS - Syntax for defining Links with Classes Ben Amada HTML 8 01-16-2004 06:38 AM
Defining classes in jscript Henke ASP .Net 3 11-28-2003 04:18 PM



Advertisments