oop in python

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

    #1

    oop in python


    hello over there!
    I have the following question:
    Suppose I created a class: class Point:
    pass
    then instanciated an instance: new = Point()
    So now how to get the instance new as a string: like ' new ' ; Is
    there any built in function or method
    for eg:

    class Point:
    def _func_that_we_w ant_(self):
    return .......
    new._func_that_ we_want_() ---> ' new '

  • Uwe Hoffmann

    #2
    Re: oop in python

    novice schrieb:
    [color=blue]
    > class Point:
    > def _func_that_we_w ant_(self):
    > return .......[/color]


    return self.__class__. __name__


    Comment

    • Steven D'Aprano

      #3
      Re: oop in python

      On Tue, 27 Dec 2005 02:42:18 -0800, novice wrote:
      [color=blue]
      >
      > hello over there!
      > I have the following question:
      > Suppose I created a class: class Point:
      > pass
      > then instanciated an instance: new = Point()
      > So now how to get the instance new as a string: like ' new ' ;[/color]

      'new' is not a instance object, it is a name which happens to be bound to
      an instance object.

      new = Point() # bind 'new' to an instance
      new = 12 # now bind to an int
      new = float(new) # and now bind to a float
      del new # delete the name 'new'
      new = "Spanish Inquisition" # bind 'new' to a string

      Names can be bound to any object, but objects can't tell what name (or
      names!) they are bound to.

      foo = bar = Point()
      # both 'foo' and 'bar' are bound to the same instance
      [color=blue]
      > Is there any built in function or method
      > for eg:
      >
      > class Point:
      > def _func_that_we_w ant_(self):
      > return .......
      > new._func_that_ we_want_() ---> ' new '[/color]

      This is generally impossible, and here is why:

      foo = Point() # create an instance of Point and bind it to the name 'foo'
      bar = foo # 'bar' also is bound to the same instance
      parrot = spam = eggs = bar
      # we have five names bound to the _same_ instance (NOT five copies)
      del foo # now we have only four

      If there was a method "_func_that_we_ want_" that returns the name of the
      instance, what should it return? bar, parrot, spam or eggs?

      Objects can't tell what names they are bound to.

      If you tell us what you hoped to do with the name once you had it, perhaps
      we can suggest something else.


      --
      Steven.

      Comment

      • Larry Bates

        #4
        Re: oop in python

        novice wrote:[color=blue]
        > hello over there!
        > I have the following question:
        > Suppose I created a class: class Point:
        > pass
        > then instanciated an instance: new = Point()
        > So now how to get the instance new as a string: like ' new ' ; Is
        > there any built in function or method
        > for eg:
        >
        > class Point:
        > def _func_that_we_w ant_(self):
        > return .......
        > new._func_that_ we_want_() ---> ' new '
        >[/color]

        Just a wild guess, but I think he wants to refer to it by a
        name. The best way to do this is to put the class instance
        in a dictionary and make the key 'new'

        instancedict={}
        instancedict['new']=Point()

        then you can call methods by:

        instancedict['new'].somemethod()

        -Larry Bates

        Comment

        • jmdeschamps@gmail.com

          #5
          Re: oop in python


          Larry Bates wrote:[color=blue]
          > novice wrote:[color=green]
          > > hello over there!
          > > I have the following question:
          > > Suppose I created a class: class Point:
          > > pass
          > > then instanciated an instance: new = Point()
          > > So now how to get the instance new as a string: like ' new ' ; Is
          > > there any built in function or method
          > > for eg:
          > >
          > > class Point:
          > > def _func_that_we_w ant_(self):
          > > return .......
          > > new._func_that_ we_want_() ---> ' new '
          > >[/color]
          >
          > Just a wild guess, but I think he wants to refer to it by a
          > name. The best way to do this is to put the class instance
          > in a dictionary and make the key 'new'
          >
          > instancedict={}
          > instancedict['new']=Point()
          >
          > then you can call methods by:
          >
          > instancedict['new'].somemethod()
          >
          > -Larry Bates[/color]

          Maybe it's more in the way of the builtin function *eval*...
          example (you decide if this is what you mean ;-) )[color=blue][color=green][color=darkred]
          >>> class toto(object):[/color][/color][/color]
          .... def max(self):
          .... return 12
          ....[color=blue][color=green][color=darkred]
          >>> t=toto()
          >>> eval("t.max")[/color][/color][/color]
          <bound method toto.max of <__main__.tot o object at 0x00D4BB90>>[color=blue][color=green][color=darkred]
          >>> eval("t.max()")[/color][/color][/color]
          12

          Comment

          • jmdeschamps@gmail.com

            #6
            Re: oop in python

            (addendum) ... And even ...
            [color=blue][color=green][color=darkred]
            >>> eval("t").max()[/color][/color][/color]
            12[color=blue][color=green][color=darkred]
            >>>[/color][/color][/color]

            Comment

            Working...