defining classes

Discussion in 'Python' started by LeRoy Lee, Sep 2, 2005.

  1. LeRoy Lee

    LeRoy Lee Guest

    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/go/onm00200471ave/direct/01/
     
    LeRoy Lee, Sep 2, 2005
    #1
    1. Advertising

  2. 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
     
    Michael Hoffman, Sep 2, 2005
    #2
    1. Advertising

  3. On 2005-09-02, LeRoy Lee <> 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
     
    Grant Edwards, Sep 2, 2005
    #3
  4. 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
     
    Steve Horsley, Sep 2, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Henke

    Defining classes in jscript

    Henke, Nov 28, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    414
    Hans Kesting
    Nov 28, 2003
  2. Ben Amada
    Replies:
    8
    Views:
    3,195
    Ben Amada
    Jan 16, 2004
  3. Joe
    Replies:
    29
    Views:
    874
    Stewart Gordon
    Mar 4, 2004
  4. johny smith
    Replies:
    8
    Views:
    421
    Peter Koch Larsen
    Jul 2, 2004
  5. vasudevram
    Replies:
    6
    Views:
    926
    vasudevram
    Apr 24, 2013
Loading...

Share This Page