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  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to