oop in python

N

novice

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_want_(self):
return .......
new._func_that_we_want_() ---> ' new '
 
S

Steven D'Aprano

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.
 
L

Larry Bates

novice said:
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_want_(self):
return .......
new._func_that_we_want_() ---> ' new '

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
 
J

jmdeschamps

Larry said:
novice said:
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_want_(self):
return .......
new._func_that_we_want_() ---> ' new '

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

Maybe it's more in the way of the builtin function *eval*...
example (you decide if this is what you mean ;-) ).... def max(self):
.... return 12
....12
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top