object creation

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

    object creation

    for record in roll:
    x = box()
    x.createSomethi ng(record)
    do something ....


    Can anyone tell me why python keeps return the original object x that
    was created in the FOR loop. I want to instantiate a new x object for
    each iteration of the FOR loop

  • George Sakkis

    #2
    Re: object creation

    On Nov 14, 5:16 pm, BiraRai <bira...@gmail. comwrote:
    for record in roll:
        x = box()
        x.createSomethi ng(record)
        do something ....
    >
    Can anyone tell me why python keeps return the original object x that
    was created in the FOR loop.  I want to instantiate a new x object for
    each iteration of the FOR loop
    What is box()? Pasting its definition would help.

    George

    Comment

    • BiraRai

      #3
      Re: object creation

      On Nov 14, 5:44 pm, George Sakkis <george.sak...@ gmail.comwrote:
      On Nov 14, 5:16 pm, BiraRai <bira...@gmail. comwrote:
      >
      for record in roll:
          x = box()
          x.createSomethi ng(record)
          do something ....
      >
      Can anyone tell me why python keeps return the original object x that
      was created in the FOR loop.  I want to instantiate a new x object for
      each iteration of the FOR loop
      >
      What is box()? Pasting its definition would help.
      >
      George
      class box:
      a = int()
      b = int()

      def createSomething (self,x):


      Comment

      • Jerry Hill

        #4
        Re: object creation

        On Fri, Nov 14, 2008 at 6:10 PM, BiraRai <birarai@gmail. comwrote:
        class box:
        a = int()
        b = int()
        >
        def createSomething (self,x):
        At a guess, x = box() does create a new instance of your box class,
        but since you've declared a and b to be class variables instead of
        instance variables, it doesn't look that way to you.

        Try adding the line:
        print id(x)
        after you call x = box(), and you should see that.

        Then add
        def __init__(self):
        a = 0
        b = 0

        to your box class to make a and b instance variables.

        --
        Jerry

        Comment

        • Joe Strout

          #5
          Re: object creation

          On Nov 14, 2008, at 4:33 PM, Jerry Hill wrote:
          Then add
          def __init__(self):
          a = 0
          b = 0
          >
          to your box class to make a and b instance variables.
          Doesn't that have to be "self.a" and "self.b"?

          Best,
          - Joe

          Comment

          • Steven D'Aprano

            #6
            Re: object creation

            On Fri, 14 Nov 2008 16:38:15 -0700, Joe Strout wrote:
            On Nov 14, 2008, at 4:33 PM, Jerry Hill wrote:
            >
            >Then add
            >def __init__(self):
            > a = 0
            > b = 0
            >>
            >to your box class to make a and b instance variables.
            >
            Doesn't that have to be "self.a" and "self.b"?

            Only if you want it to work :)




            --
            Steven

            Comment

            • Jerry Hill

              #7
              Re: object creation

              On Fri, Nov 14, 2008 at 6:38 PM, Joe Strout <joe@strout.net wrote:
              On Nov 14, 2008, at 4:33 PM, Jerry Hill wrote:
              >
              >Then add
              >def __init__(self):
              > a = 0
              > b = 0
              >
              Doesn't that have to be "self.a" and "self.b"?
              Yes, that should indeed have been self.a and self.b! Sorry about that.

              --
              Jerry

              Comment

              Working...