defining classes

Grant Edwards grante at visi.com
Fri Sep 2 16:19:24 EDT 2005


On 2005-09-02, LeRoy Lee <l3e3e7 at hotmail.com> 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            



More information about the Python-list mailing list