Question about Objects

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

    Question about Objects

    I've been hacking visual basic for several years and understand the basic
    concepts of OOP. That said, I'm stumped here with the Python Class.

    Here is the Class...
    [color=blue]
    >class Test:
    > def __init__(self, something):
    > self.something = something
    >
    > def getSomething(se lf):
    > return self.something[/color]

    This is what I get when I test it. Why does <getSomething > not return the
    value of <something>? is obvious that <something> has a value. I fear this
    is a simple oversight but I've racked my brain for hours looking at online
    doc's and examples. Thanks for any help!!
    [color=blue]
    >Python 2.3 (#46, Jul 29 2003, 18:54:32) [MSC v.1200 32 bit (Intel)] on[/color]
    win32[color=blue]
    >Type "copyright" , "credits" or "license()" for more information.
    >************** *************** *************** *************** *****
    >IDLE 1.0 ==== No Subprocess ====[color=green][color=darkred]
    >>>>
    >>>> x = Test("Microsoft Sucks")
    >>>> x.getSomething[/color][/color]
    ><bound method Test.getSomethi ng of <__main__.Tes t instance at 0x00C01940>>[color=green][color=darkred]
    >>>> x.something[/color][/color]
    >'Microsoft Sucks'[color=green][color=darkred]
    >>>>[/color][/color][/color]


  • Mark Roach

    #2
    Re: Question about Objects

    On Fri, 21 Nov 2003 09:56:07 -0500, campbell95 wrote:
    [color=blue]
    > I've been hacking visual basic for several years and understand the basic
    > concepts of OOP. That said, I'm stumped here with the Python Class.[/color]
    [...][color=blue][color=green]
    >> def getSomething(se lf):
    >> return self.something[/color][/color]
    [...][color=blue][color=green][color=darkred]
    >>>>> x.getSomething[/color]
    >><bound method Test.getSomethi ng of <__main__.Tes t instance at 0x00C01940>>[/color][/color]

    you need to actually call the method: x.getSomething( )

    -Mark

    Comment

    • Fernando Rodriguez

      #3
      Re: Question about Objects

      On Fri, 21 Nov 2003 09:56:07 -0500, "campbell95 " <campbell95@cox .net> wrote:
      [color=blue]
      >This is what I get when I test it. Why does <getSomething > not return the
      >value of <something>? is obvious that <something> has a value. I fear this
      >is a simple oversight but I've racked my brain for hours looking at online[/color]

      It is. ;-)
      [color=blue]
      >doc's and examples. Thanks for any help!!
      >[color=green]
      >>Python 2.3 (#46, Jul 29 2003, 18:54:32) [MSC v.1200 32 bit (Intel)] on[/color]
      >win32[color=green]
      >>Type "copyright" , "credits" or "license()" for more information.
      >>************* *************** *************** *************** ******
      >>IDLE 1.0 ==== No Subprocess ====[color=darkred]
      >>>>>
      >>>>> x = Test("Microsoft Sucks")
      >>>>> x.getSomething[/color]
      >><bound method Test.getSomethi ng of <__main__.Tes t instance at 0x00C01940>>[/color][/color]

      Remember, this isn't VB: you can't leave the parens off when calling a method.
      There's no difference between subs and functions in Python, and you must
      include the parens if you want to _call_ the function and get the value.
      Otherwise you get the function.

      It makes sense, it just that you're used to the peculiar MS-way of doing
      things. Your mind will heal, don't worry. ;-)

      Comment

      • bas68

        #4
        Re: Question about Objects

        Great community, glad I stopped in. Thanks for all the help!

        "Fernando Rodriguez" <frr@easyjob.ne t> wrote in message
        news:phcsrv08sl g5tdoquvk6b5ve4 8bgf0i5vp@4ax.c om...[color=blue]
        > On Fri, 21 Nov 2003 09:56:07 -0500, "campbell95 " <campbell95@cox .net>[/color]
        wrote:[color=blue]
        >[color=green]
        > >This is what I get when I test it. Why does <getSomething > not return the
        > >value of <something>? is obvious that <something> has a value. I fear[/color][/color]
        this[color=blue][color=green]
        > >is a simple oversight but I've racked my brain for hours looking at[/color][/color]
        online[color=blue]
        >
        > It is. ;-)
        >[color=green]
        > >doc's and examples. Thanks for any help!!
        > >[color=darkred]
        > >>Python 2.3 (#46, Jul 29 2003, 18:54:32) [MSC v.1200 32 bit (Intel)] on[/color]
        > >win32[color=darkred]
        > >>Type "copyright" , "credits" or "license()" for more information.
        > >>************* *************** *************** *************** ******
        > >>IDLE 1.0 ==== No Subprocess ====
        > >>>>>
        > >>>>> x = Test("Microsoft Sucks")
        > >>>>> x.getSomething
        > >><bound method Test.getSomethi ng of <__main__.Tes t instance at[/color][/color][/color]
        0x00C01940>>[color=blue]
        >
        > Remember, this isn't VB: you can't leave the parens off when calling a[/color]
        method.[color=blue]
        > There's no difference between subs and functions in Python, and you must
        > include the parens if you want to _call_ the function and get the value.
        > Otherwise you get the function.
        >
        > It makes sense, it just that you're used to the peculiar MS-way of doing
        > things. Your mind will heal, don't worry. ;-)
        >[/color]


        Comment

        • Bruno Desthuilliers

          #5
          Re: Question about Objects

          bas68 wrote:[color=blue]
          > Great community, glad I stopped in. Thanks for all the help!
          >[/color]
          You're welcome !-) And yes, this is probably one of the friendliest
          places on usenet (thanks you all python-lovers BTW).

          (But please edit out irrelevant parts of the posts you're answering to
          !-)

          (snip other posts)

          Bruno

          Comment

          Working...