I'm sorry, name of instance inside its methods 
Author Message
 I'm sorry, name of instance inside its methods

actually you're correct, I want the name of the instance
not the name of the class

Quote:

> >       def classname(self):
> >               <blank>

> > -------------------------------

> > >>>from hello import *
> > >>>clown = Hello(1)
> > >>>clown.classname()
> > clown

> hmmm.... looking at that again, "clown" is an instance name, not the
> name of the class.. which do you actually want?

> Cheers,

> - Michal



Thu, 15 Aug 2002 03:00:00 GMT  
 I'm sorry, name of instance inside its methods
In that case, previous posts would seem to indicate there is a technical
name for your position: you are "screwed".  Sorry.

regards
 Steve

Quote:

> actually you're correct, I want the name of the instance
> not the name of the class

> > >             def classname(self):
> > >                     <blank>

> > > -------------------------------

> > > >>>from hello import *
> > > >>>clown = Hello(1)
> > > >>>clown.classname()
> > > clown

> > hmmm.... looking at that again, "clown" is an instance name, not the
> > name of the class.. which do you actually want?

> > Cheers,

> > - Michal

--
"If computing ever stops being fun, I'll stop doing it"


Fri, 16 Aug 2002 03:00:00 GMT  
 I'm sorry, name of instance inside its methods


Quote:
>actually you're correct, I want the name of the instance
>not the name of the class

>> >             def classname(self):
>> >                     <blank>

>> > -------------------------------

>> > >>>from hello import *
>> > >>>clown = Hello(1)
>> > >>>clown.classname()
>> > clown

>> hmmm.... looking at that again, "clown" is an instance name, not the
>> name of the class.. which do you actually want?

class Hello:
   def __init__( self, name ):
      self.__name = name
   def instanceName( self ):
      return self.__name

clown = Hello( "clown" )
print clown.instanceName()

Will something like that work?

--
When a thing ceases to be a subject of controversy,
it ceases to be a subject of interest.
                                                   -- William Hazlitt



Fri, 16 Aug 2002 03:00:00 GMT  
 I'm sorry, name of instance inside its methods
This question is related to OOP approach.
Why do I have to :
        def __init__(self, name):
                self.__name = name

        def getname(self):
                return self.__name
and
x= MyObject.getname()

instead of
        def __init__(self, name):
                name= name
and then
x= MyObject.name

--
Life is not fair
But the root password helps
--

Gregoire Welraeds

Perceval Development team
-------------------------------------------------------------------------------
Perceval Technologies sa/nv     Tel: +32-2-6409194              
Rue Tenbosch, 9                 Fax: +32-2-6403154              


URL: http://www.perceval.be/
-------------------------------------------------------------------------------



Fri, 16 Aug 2002 03:00:00 GMT  
 I'm sorry, name of instance inside its methods


 >  This question is related to OOP approach.
 >  Why do I have to :
 >      def __init__(self, name):
 >              self.__name = name
 >  
 >      def getname(self):
 >              return self.__name
 >  and
 >  x= MyObject.getname()
 >  
 >  instead of
 >      def __init__(self, name):
 >              name= name
 >  and then
 >  x= MyObject.name

What's wrong with the following?

      def __init__(self, name):
              self.name = name

and then:

x = MyObject.name

/Mikael

-----------------------------------------------------------------------

WWW:     http://www.dtr.isy.liu.se/dtr/staff/mikael
Phone:   +46 - (0)13 - 28 1343
Telefax: +46 - (0)13 - 28 1339
Date:    28-Feb-00
Time:    15:20:02

This message was sent by XF-Mail.
-----------------------------------------------------------------------



Fri, 16 Aug 2002 03:00:00 GMT  
 I'm sorry, name of instance inside its methods
Quote:

> This question is related to OOP approach.
> Why do I have to :
>         def __init__(self, name):
>                 self.__name = name

The self argument provdes the necessary object instance context for
retrieval of the required instance variable. Class methods are
special functions, in that the instance references are silently
provided as an extra first argument.

Quote:
>         def getname(self):
>                 return self.__name
> and
> x= MyObject.getname()

Yes, this all seems to work nicely.

Quote:
> instead of
>         def __init__(self, name):
>                 name= name

Here you will run up against the three-scopes rule: the name on the LHS
of the assignment will be a variable local to the method, and thus not
available after the __init__ method completes.

Quote:
> and then
> x= MyObject.name

There's nothing Pythonically wrong with using instance variables this way.
Using get/set routines gives better decoupling and allows you to change
your objects' implementations without breaking surrounding code.  So there
is nothing *technically* wrong with:

Quote:
>>> class Foo:

        def __init__(self):
                self.localvar = 177

Quote:
>>> bar = Foo()
>>> bar.localvar
177

It's just a question of whether you want to allow consumers of object
services to "reach behind" the interfaces in this way.  In this respect
python is much more relaxed than Java or C++, which enforce restrictions
on attribute accessibility.  In a typeless language it's more appropriate
to leave it to the programmer's discretion.

Quote:
> --
> Life is not fair
> But the root password helps
> --

> Gregoire Welraeds

Python makes it easy to write good programs rather than difficult to
write bad ones.

regards
 Steve
--
"If computing ever stops being fun, I'll stop doing it"



Fri, 16 Aug 2002 03:00:00 GMT  
 I'm sorry, name of instance inside its methods

I am interested in joining a E-mail list where COM-Win32 issues are
discussed. I am currently strapped to MS platform but interested in jumping
ship to Linux in due time.

Sincerely,

Adolfo Aguirre
MIS
Mercury Press Intl.
Santa Barbara, CA

805-452-2322



Mon, 19 Aug 2002 03:00:00 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. name of object inside it's methods?

2. Singleton methods and instance methods with same name?

3. get instance name inside of member function

4. Invoking a subclass's method on its superclass's instance

5. Method to give name of class instance?

6. Name of a method of an instance

7. Finding the current 'method' name

8. Why can't instance methods be pickled?

9. looking up an instance's methods

10. instance creations methods for VW's Image class

11. defining a method inside of another method

12. Getting an instance's class name?

 

 
Powered by phpBB® Forum Software