oop in python

Steven D'Aprano steve at REMOVETHIScyber.com.au
Tue Dec 27 12:00:46 EST 2005


On Tue, 27 Dec 2005 02:42:18 -0800, novice wrote:

> 
> 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 '   ;

'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

> Is there any built in function or method
>  for eg:
> 
> class Point:
>        def _func_that_we_want_(self):
>           return .......
> new._func_that_we_want_()  --->  ' new '

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.




More information about the Python-list mailing list