[Tutor] How to have the name of a function inside the code of this function?

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

[Tutor] How to have the name of a function inside the code of this function?

Karim

Hello all,


I have :

     def foo():
            print( getattr(foo, 'func_name'))

Where I get the name of the function but I add to give the name of this
function. Indeed it is not very helpful...
I checked the globals() but how I can do to get
globals()['toto'].func_name. This is not more helpful ;o)

If you have any idea to get the caller name inside the caller.

Cheers
Karim
_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] How to have the name of a function inside the code of this function?

Steven D'Aprano-8
Karim wrote:
>
> Hello all,
>
>
> I have :
>
>     def foo():
>            print( getattr(foo, 'func_name'))

Why not this?

def foo():
     print 'foo'


You already know the name of the function.

There is no portable way of retrieving the name of the current function from
within that function.



--
Steven

_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] How to have the name of a function inside the code of this function?

modulok-2
In reply to this post by Karim
On 4/6/12, Karim <[hidden email]> wrote:

>
> Hello all,
>
>
> I have :
>
>      def foo():
>             print( getattr(foo, 'func_name'))
>
> Where I get the name of the function but I add to give the name of this
> function. Indeed it is not very helpful...
> I checked the globals() but how I can do to get
> globals()['toto'].func_name. This is not more helpful ;o)
>
> If you have any idea to get the caller name inside the caller.
>

The following works, but only on implementations which provide stack frame
support. As the docs kindly point out:

    "...this isn't guaranteed to exist in all implementations of Python."

Example code:


    import inspect
    def foo():
        '''Print my own name.'''
        frame_info = inspect.getframeinfo(inspect.currentframe())
        print(frame_info.function)


    foo()


That said, there's probably a better way to solve whatever bigger problem
you're trying solve.

-Modulok-
_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] How to have the name of a function inside the code of this function?

Alan Gauld
In reply to this post by Karim
On 06/04/12 09:47, Karim wrote:

> If you have any idea to get the caller name inside the caller.


Its not normally very helpful since in Python the same function can have
many names:

def F(x):
    return x*x

a = F
b = F
c - lambda y: F(y)

print F(1), a(2), b(3), c(4)

Now, why would knowing whether the caller used F,a or b
to call the same function object help? And what do you
do in the case of c()? Do you return 'c' or 'F'?

Maybe you could use it to tell you the context from
which they were calling? But in that case there are
usually better, more reliable, techniques
  - like examining the stackframe.

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

_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] How to have the name of a function inside the code of this function?

Karim
Le 06/04/2012 19:31, Alan Gauld a écrit :

> On 06/04/12 09:47, Karim wrote:
>
>> If you have any idea to get the caller name inside the caller.
>
>
> Its not normally very helpful since in Python the same function can
> have many names:
>
> def F(x):
>    return x*x
>
> a = F
> b = F
> c - lambda y: F(y)
>
> print F(1), a(2), b(3), c(4)
>
> Now, why would knowing whether the caller used F,a or b
> to call the same function object help? And what do you
> do in the case of c()? Do you return 'c' or 'F'?
>
> Maybe you could use it to tell you the context from
> which they were calling? But in that case there are
> usually better, more reliable, techniques
>  - like examining the stackframe.
>
> HTH,

Thanks Steven, Moduok and Steven for all your answers!

The reason is simple I wanted to optimize some code using pyuno for
openoffice.org doc generation I have several methods to set
text with "Heading 1", ... "Heading <N>" title style:

def title(self, text='', style="Heading 1"):
      self._cursor_text.setPropertyValue('ParaStyleName', style)
      self.add_text(text)

def title1(self, text=''):
      self.title(text=text)

def title2(self, text=''):
      self.title(text='', style="Heading 2")

...

def title9(self, text=''):
      self.title(text='', style="Heading 9")

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

I just wanted to improve a little by doing something like that (pseudo
code):

def title9(self, text=''):
      self.title(text='', style="Heading " + <func_name>.split()[1])


In short the number in the funtion name is the number of the title depth
in the document.
There is no big deal if Iit's not feasible;

Cheers
Karim








_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] How to have the name of a function inside the code of this function?

Dave Angel-3
On 04/06/2012 03:19 PM, Karim wrote:

> Le 06/04/2012 19:31, Alan Gauld a écrit :
>> On 06/04/12 09:47, Karim wrote:
>>
>>> If you have any idea to get the caller name inside the caller.
>>
>>
>> Its not normally very helpful since in Python the same function can
>> have many names:
>>
>> def F(x):
>>    return x*x
>>
>> a = F
>> b = F
>> c - lambda y: F(y)
>>
>> print F(1), a(2), b(3), c(4)
>>
>> Now, why would knowing whether the caller used F,a or b
>> to call the same function object help? And what do you
>> do in the case of c()? Do you return 'c' or 'F'?
>>
>> Maybe you could use it to tell you the context from
>> which they were calling? But in that case there are
>> usually better, more reliable, techniques
>>  - like examining the stackframe.
>>
>> HTH,
>
> Thanks Steven, Moduok and Steven for all your answers!
>
> The reason is simple I wanted to optimize some code using pyuno for
> openoffice.org doc generation I have several methods to set
> text with "Heading 1", ... "Heading <N>" title style:
>
> def title(self, text='', style="Heading 1"):
>      self._cursor_text.setPropertyValue('ParaStyleName', style)
>      self.add_text(text)
>
> def title1(self, text=''):
>      self.title(text=text)
>
> def title2(self, text=''):
>      self.title(text='', style="Heading 2")
>
> ...
>
> def title9(self, text=''):
>      self.title(text='', style="Heading 9")
>
> ---------------------------------------------------------------------------------
>
>
> I just wanted to improve a little by doing something like that (pseudo
> code):
>
> def title9(self, text=''):
>      self.title(text='', style="Heading " + <func_name>.split()[1])
>
>
> In short the number in the funtion name is the number of the title
> depth in the document.
> There is no big deal if Iit's not feasible;
>
> Cheers
> Karim
>
>
>

Those are methods, not functions.  So if you have a bunch of methods,
differing only in the numeric suffix their names have, and one of the
parameters to a method they each call, there's probably a simpler way.

First, you could create function objects (using approaches like
partial), turn them into methods, and attach them to a class with
generated names (somebody else will have to help you do it;  I just am
pretty sure it's possible)

Second, ifyou can control the code which will be calling these methods,
you could just have that code parameterize things a little differently.
For example, instead of calling

   obj.title9("my text")

it might call
    obj.title(9, "my text")

where title() is a pretty simple, single method.





--

DaveA

_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] How to have the name of a function inside the code of this function?

Karim
Le 07/04/2012 04:01, Dave Angel a écrit :

> On 04/06/2012 03:19 PM, Karim wrote:
>> Le 06/04/2012 19:31, Alan Gauld a écrit :
>>> On 06/04/12 09:47, Karim wrote:
>>>
>>>> If you have any idea to get the caller name inside the caller.
>>>
>>> Its not normally very helpful since in Python the same function can
>>> have many names:
>>>
>>> def F(x):
>>>     return x*x
>>>
>>> a = F
>>> b = F
>>> c - lambda y: F(y)
>>>
>>> print F(1), a(2), b(3), c(4)
>>>
>>> Now, why would knowing whether the caller used F,a or b
>>> to call the same function object help? And what do you
>>> do in the case of c()? Do you return 'c' or 'F'?
>>>
>>> Maybe you could use it to tell you the context from
>>> which they were calling? But in that case there are
>>> usually better, more reliable, techniques
>>>   - like examining the stackframe.
>>>
>>> HTH,
>> Thanks Steven, Moduok and Steven for all your answers!
>>
>> The reason is simple I wanted to optimize some code using pyuno for
>> openoffice.org doc generation I have several methods to set
>> text with "Heading 1", ... "Heading<N>" title style:
>>
>> def title(self, text='', style="Heading 1"):
>>       self._cursor_text.setPropertyValue('ParaStyleName', style)
>>       self.add_text(text)
>>
>> def title1(self, text=''):
>>       self.title(text=text)
>>
>> def title2(self, text=''):
>>       self.title(text='', style="Heading 2")
>>
>> ...
>>
>> def title9(self, text=''):
>>       self.title(text='', style="Heading 9")
>>
>> ---------------------------------------------------------------------------------
>>
>>
>> I just wanted to improve a little by doing something like that (pseudo
>> code):
>>
>> def title9(self, text=''):
>>       self.title(text='', style="Heading " +<func_name>.split()[1])
>>
>>
>> In short the number in the funtion name is the number of the title
>> depth in the document.
>> There is no big deal if Iit's not feasible;
>>
>> Cheers
>> Karim
>>
>>
>>
> Those are methods, not functions.  So if you have a bunch of methods,
> differing only in the numeric suffix their names have, and one of the
> parameters to a method they each call, there's probably a simpler way.
>
> First, you could create function objects (using approaches like
> partial), turn them into methods, and attach them to a class with
> generated names (somebody else will have to help you do it;  I just am
> pretty sure it's possible)
>
> Second, ifyou can control the code which will be calling these methods,
> you could just have that code parameterize things a little differently.
> For example, instead of calling
>
>     obj.title9("my text")
>
> it might call
>      obj.title(9, "my text")
>
> where title() is a pretty simple, single method.
>


Thanks Dave,

In fact at first I did that:

obj.title(text='my text', heading=9)

But I wanted something more flashing to recognize and more simple to
write because I've got a lot of call in my 1000 pages document creation.

I will take a look at partial.

Cheers

PS: By the I thanked Steven twice this one an mistake sorry and thank
you Alan!
_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor