Re: [Tutor] How to have the name of a function inside the code of this function?
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" 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 " +.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
Re: [Tutor] How to have the name of a function inside the code of this function?
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 " 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 " + .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 - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to have the name of a function inside the code of this function?
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 " 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 " + .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 - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to have the name of a function inside the code of this function?
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 - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to have the name of a function inside the code of this function?
On 4/6/12, Karim 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 - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to have the name of a function inside the code of this function?
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 - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] How to have the name of a function inside the code of this function?
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 - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor