defining classes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • LeRoy Lee

    #1

    defining classes

    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
    [color=blue][color=green]
    >>h = foo1(1)
    >>h.r[/color][/color]
    1[color=blue][color=green]
    >>h.j[/color][/color]
    5


    Now take this example

    class foo2:
    def __init__(self):
    self.j = 5
    [color=blue][color=green]
    >>h = foo2()
    >>h.j[/color][/color]
    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!


  • Michael Hoffman

    #2
    Re: defining classes

    LeRoy Lee wrote:
    [color=blue]
    > class foo2:
    > def __init__(self):
    > self.j = 5
    >[color=green][color=darkred]
    >>> h = foo2()
    >>> h.j[/color][/color]
    >
    > Traceback (most recent call last):
    > File "<stdin>", line 1, in ?
    > AttributeError: foo2 instance has no attribute 'j'[/color]

    Try again:
    [color=blue][color=green][color=darkred]
    >>> class foo2:[/color][/color][/color]
    .... def __init__(self):
    .... self.j = 5
    ....[color=blue][color=green][color=darkred]
    >>> h = foo2()
    >>> h.j[/color][/color][/color]
    5
    --
    Michael Hoffman

    Comment

    • Grant Edwards

      #3
      Re: defining classes

      On 2005-09-02, LeRoy Lee <l3e3e7@hotmail .com> wrote:
      [color=blue]
      > Now take this example
      >
      > class foo2:
      > def __init__(self):
      > self.j = 5
      >[color=green][color=darkred]
      >>>h = foo2()
      >>>h.j[/color][/color]
      > Traceback (most recent call last):
      > File "<stdin>", line 1, in ?
      > AttributeError: foo2 instance has no attribute 'j'[/color]

      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.[color=blue][color=green][color=darkred]
      >>> class foo2:[/color][/color][/color]
      ... def __init__(self):
      ... self.j = 5
      ...[color=blue][color=green][color=darkred]
      >>> h = foo2()
      >>> h.j[/color][/color][/color]
      5[color=blue][color=green][color=darkred]
      >>>[/color][/color][/color]

      --
      Grant Edwards grante Yow! I'm definitely not
      at in Omaha!
      visi.com

      Comment

      • Steve Horsley

        #4
        Re: defining classes

        LeRoy Lee wrote:[color=blue]
        > 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
        >[color=green][color=darkred]
        >>> h = foo1(1)
        >>> h.r[/color][/color]
        >
        > 1
        >[color=green][color=darkred]
        >>> h.j[/color][/color]
        >
        > 5
        >
        >
        > Now take this example
        >
        > class foo2:
        > def __init__(self):
        > self.j = 5
        >[color=green][color=darkred]
        >>> h = foo2()
        >>> h.j[/color][/color]
        >
        > 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?
        >[/color]

        It works for me.
        [color=blue][color=green][color=darkred]
        >>> class foo2:[/color][/color][/color]
        .... def __init__(self):
        .... self.j = 5
        ....[color=blue][color=green][color=darkred]
        >>> h = foo2()
        >>> h.j[/color][/color][/color]
        5[color=blue][color=green][color=darkred]
        >>>[/color][/color][/color]

        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

        Comment

        Working...