[Tutor] print method name

2011-12-08 Thread rail shafigulin
i created a class and in some instances when i use it call some of its
methods i need to print a method name. the online search did produce some
results but none of them seem to work for me. for example one of them said
just to use __name__ or func_name but it didn't work for me. i'm using
python 3.1.1

any help is appreciated.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] print method name

2011-12-08 Thread James Reynolds
On Thu, Dec 8, 2011 at 9:49 AM, rail shafigulin
rail.shafigu...@gmail.comwrote:

 i created a class and in some instances when i use it call some of its
 methods i need to print a method name. the online search did produce some
 results but none of them seem to work for me. for example one of them said
 just to use __name__ or func_name but it didn't work for me. i'm using
 python 3.1.1

 any help is appreciated.

 ___
 Tutor maillist  -  Tutor@python.org
 To unsubscribe or change subscription options:
 http://mail.python.org/mailman/listinfo/tutor


Can you show the code?

This works for me:

class A(object):
def __init__(self):
pass

def addr(self,a,b):
return a + b

a = A()
print a.addr.__name__

addr
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] print method name

2011-12-08 Thread Joel Goldstick
On Thu, Dec 8, 2011 at 9:49 AM, rail shafigulin
rail.shafigu...@gmail.comwrote:

 i created a class and in some instances when i use it call some of its
 methods i need to print a method name. the online search did produce some
 results but none of them seem to work for me. for example one of them said
 just to use __name__ or func_name but it didn't work for me. i'm using
 python 3.1.1

 any help is appreciated.

 It would be better if you send us the code you tried.  I'm not sure what
you are looking to do.  When you invoke the method, do you want it to print
its own name?


class A(object):
...   def first(self):
... pass
...
 a = A()
 a.first.__name__
'first'


 ___
 Tutor maillist  -  Tutor@python.org
 To unsubscribe or change subscription options:
 http://mail.python.org/mailman/listinfo/tutor




-- 
Joel Goldstick
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] print method name

2011-12-08 Thread Lie Ryan

On 12/09/2011 01:49 AM, rail shafigulin wrote:

i created a class and in some instances when i use it call some of its
methods i need to print a method name. the online search did produce
some results but none of them seem to work for me. for example one of
them said just to use __name__ or func_name but it didn't work for me.
i'm using python 3.1.1



I'm guessing that you're  doing something like

def foo():
...

print foo().__name__

foo() would call the foo() function, so you'd be asking the .__name__ of 
the object returned by foo not the __name__ of foo. Instead you'd need 
to use the the function object itself, IOW don't call foo just do:


print foo.__name__


This should work for both python 2 and python 3, and for both class 
methods or regular functions.


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] print method name

2011-12-08 Thread rail shafigulin
On Thu, Dec 8, 2011 at 10:17 AM, Joel Goldstick joel.goldst...@gmail.comwrote:



 On Thu, Dec 8, 2011 at 9:49 AM, rail shafigulin rail.shafigu...@gmail.com
  wrote:

 i created a class and in some instances when i use it call some of its
 methods i need to print a method name. the online search did produce some
 results but none of them seem to work for me. for example one of them said
 just to use __name__ or func_name but it didn't work for me. i'm using
 python 3.1.1

 any help is appreciated.

 It would be better if you send us the code you tried.  I'm not sure what
 you are looking to do.  When you invoke the method, do you want it to print
 its own name?


 class A(object):
 ...   def first(self):
 ... pass
 ...
  a = A()
  a.first.__name__
 'first'


 ___
 Tutor maillist  -  Tutor@python.org
 To unsubscribe or change subscription options:
 http://mail.python.org/mailman/listinfo/tutor




 --
 Joel Goldstick


folks, never mind. i'm not sure what went wrong when i ran my code and got
an error. i ran it again and it is working fine. my sincere apologies.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] print method name

2011-12-08 Thread rail shafigulin
On Thu, Dec 8, 2011 at 10:40 AM, Lie Ryan lie.1...@gmail.com wrote:

 On 12/09/2011 01:49 AM, rail shafigulin wrote:

 i created a class and in some instances when i use it call some of its
 methods i need to print a method name. the online search did produce
 some results but none of them seem to work for me. for example one of
 them said just to use __name__ or func_name but it didn't work for me.
 i'm using python 3.1.1


 I'm guessing that you're  doing something like

 def foo():
...

 print foo().__name__

 foo() would call the foo() function, so you'd be asking the .__name__ of
 the object returned by foo not the __name__ of foo. Instead you'd need to
 use the the function object itself, IOW don't call foo just do:

 print foo.__name__


 This should work for both python 2 and python 3, and for both class
 methods or regular functions.


 __**_
 Tutor maillist  -  Tutor@python.org
 To unsubscribe or change subscription options:
 http://mail.python.org/**mailman/listinfo/tutorhttp://mail.python.org/mailman/listinfo/tutor


as a matter of apology i found another way of getting a method name without
actually knowing the name of the method:

import inspect

class MyClass(object):
  def __init__(self):
pass

  def mymethod(self):
print(inspect.getframeinfo(inspect.currentframe()).function)

def main():
  a = MyClass()
  a.mymethod()

if __name__ == '__main__':
  main()
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] print method name

2011-12-08 Thread Alan Gauld

On 08/12/11 14:49, rail shafigulin wrote:

i created a class and in some instances when i use it call some of its
methods i need to print a method name.


I'm curious why you need this?
In most cases if you call a method you know its name...

myObj = MyClass()
myObj.spam()
print Just called MyClass.spam()

This would only be useful if you were creating references to methods 
dynamically or using callbacks from asynchronous messages or something?


Just wondering what you are doing that requires this?

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor