Emacs function to message the python function I'm in

I'm editing some Python code with rather long functions and decided it would be useful to quickly get the function name without scrolling up. I put this bit of code together to do it. Is there something built in to emacs in general, or the standard python mode in particular, which I can use instead?

(defun python-show-function-name()
  "Message the name of the function the point is in"
  (interactive)
  (save-excursion
    (beginning-of-defun)
    (message (format "%s" (thing-at-point 'line)))))


Asked by: Julia625 | Posted: 06-12-2021






Answer 1

You may find decent results with which-function-mode:

Which Function mode is a minor mode that displays the current function name in the mode line, updating it as you move around in a buffer.

To either enable or disable Which Function mode, use the command M-x which-function-mode. This command is global; it applies to all buffers, both existing ones and those yet to be created. However, it takes effect only in certain major modes, those listed in the value of which-func-modes. If the value is t, then Which Function mode applies to all major modes that know how to support it—in other words, all the major modes that support Imenu.

Although I see it getting a little confused in one Python file that I have here...

Answered by: Roland631 | Posted: 07-01-2022



Answer 2

Did you try py-beginning-of-def-or-class?

(defun python-show-function-name()
  "Message the name of the function the point is in"
  (interactive)
  (save-excursion
    (py-beginning-of-def-or-class)
    (message (format "%s" (thing-at-point 'line)))))

I find it gives me better results than your beginning-of-defun, but if that's not the problem you're having, then maybe I'm just seeing another symptom of the cause of the wonkiness in my other answer.

Answered by: Julian455 | Posted: 07-01-2022



Answer 3

C-c C-u (py-goto-block-up) might be what you want.

Answered by: Max480 | Posted: 07-01-2022



Similar questions

python - i want to call an aiogram async message handler function in another async message handler function

@dp.message_handler() async def message1(message: types.Message): if message.text == 'hi': await message.answer('hi') elif message.text='hi1': #here i want to callenter code here the top message handlers twice lenter code hereenter code hereike message1('hi'),message1('hi')


How do I get the name of a function or method from within a Python function or method?

I feel like I should know this, but I haven't been able to figure it out... I want to get the name of a method--which happens to be an integration test--from inside it so it can print out some diagnostic text. I can, of course, just hard-code the method's name in the string, but I'd like to make the test a little more DRY if possible.


Using a function to create a function in Python

I'm new to programming and I'm interested in if it's possible to use a function to create another function based in inputted information: def get_new_toy(self): new_toy = gui.multenterbox( msg = 'Enter the data for the new toy:', title = 'New Toy', fields = ('Toy Name', 'Fun for 0 to 5', 'Fun for 5 to 7', 'Fun for 7 to 10', 'Fun for over 10')) method = getattr(PotatoHead, new...


python - Method vs. function in case of simple function

I have original class (DownloadPage) and I need to add just one simple functionality (get_info). What is better approach in OOP? def get_info(page): # make simple function ... result = get_info(DownloadPage()) or class MyDownloadPage(DownloadPage): # make new class with inheritance def get_info(self): # with one method ... result =...


i have a string variable which comes with function names, how do I call the function in Python?

I have a string variable which comes with different function names, and I have a file which contains an often different set of functions which matchs the content of the string, how do I call that function in Python? Example: In File 1 def function1: ... def function2: ... def function3: ... In File 2 functionname = "function2" I need to ca...


Python: run one function until another function finishes

I have two functions, draw_ascii_spinner and findCluster(companyid). I would like to: Run findCluster(companyid) in the backround and while its processing.... Run draw_ascii_spinner until findCluster(companyid) finishes How do I begin to try to solve for this (Python 2.7)?


What is the best way to exit a function (which has no return value) in python before the function ends (e.g. a check fails)?

Let's assume an iteration in which we call a function without a return value. The way I think my program should behave is explained in this pseudocode: for element in some_list: foo(element) def foo(element): do something if check is true: do more (because check was succesful) else: return None do much much more... If I implement this in python, it both...


Python function as a function argument?

Suppose I have some code like: def myfunc(anotherfunc, extraArgs): # somehow call `anotherfunc` here, passing it the `extraArgs` pass I want to pass another existing function as the anotherfunc argument, and a list or tuple of arguments as extraArgs, and have myfunc call the passed-in function with those arguments. Is this possible? How d...


python - function name as as input of another function?

I an image processing enginner, and am using Python as a prototyping language. Most of the time, as I get thousands of images, named "imagen.jpg", n being the increment. So the main structure of my program may be seen as : def main_IP(imgRoot, stop_ncrement): name = update_the_increment(imgRoot, stop_increment) img = load_the_image(name) out_img = process_image(img) displays_images...


What is the benefit to define a function in a function in python?

I encountered this piece of python code (pasted below) on effbot and I was wondering: Why defining a function within a function? import re, htmlentitydefs ## # Removes HTML or XML character references and entities from a text string. # # @param text The HTML (or XML) source text. # @return The plain text, as a Unicode st...


python - is there any way to run a other function after a function run finish?

def foo(): pass def bar(): print 'good bay' two function like blow and now i want to run bar function after foo run finish is there some method like class use __del__? as i know in class method i can use __del__ like follow: class A(object): def __init__(self): pass def __del__(self): bar() but i ...


Is there a common way to check in Python if an object is any function type?

I have a function in Python which is iterating over the attributes returned from dir(obj), and I want to check to see if any of the objects contained within is a function, method, built-in function, etc. Normally you could use callable() for this, but I don't want to include classes. The best I've come up with so far is: isinstance(obj, (types.BuiltinFunctionType, types.FunctionTy...


python - Which is more pythonic, factory as a function in a module, or as a method on the class it creates?

I have some Python code that creates a Calendar object based on parsed VEvent objects from and iCalendar file. The calendar object just has a method that adds events as they get parsed. Now I want to create a factory function that creates a calendar from a file object, path, or URL. I've been using the iCalendar python module, w...


Is there a function in python to split a word into a list?

This question already has answers here:


Is there a function in Python to split a string without ignoring the spaces?

Is there a function in Python to split a string without ignoring the spaces in the resulting list? E.g: s="This is the string I want to split".split() gives me >>> s ['This', 'is', 'the', 'string', 'I', 'want', 'to', 'split'] I want something like ['This',' ','is',' ', 'the',' ','string', ' ', .....]


unicode - Python: Use the codecs module or use string function decode?

I have a text file that is encoded in UTF-8. I'm reading it in to analyze and plot some data. I would like the file to be read in as ascii. Would it be best to use the codecs module or use the builtin string decode method? Also, the file is divided up as a csv, so could the csv module also be a valid solution? Thanks for your help.


How do I get the name of a function or method from within a Python function or method?

I feel like I should know this, but I haven't been able to figure it out... I want to get the name of a method--which happens to be an integration test--from inside it so it can print out some diagnostic text. I can, of course, just hard-code the method's name in the string, but I'd like to make the test a little more DRY if possible.


Lambda function for classes in python?

There must be an easy way to do this, but somehow I can wrap my head around it. The best way I can describe what I want is a lambda function for a class. I have a library that expects as an argument an uninstantiated version of a class to work with. It then instantiates the class itself to work on. The problem is that I'd like to be able to dynamically create versions of the class, to pass to the library, but I can't figur...


function pointers in python

I would like to do something like the following: def add(a, b): #some code def subtract(a, b): #some code operations = [add, subtract] operations[0]( 5,3) operations[1](5,3) In python, is it possible to assign something like a function pointer?


python - Django foreign key access in save() function

Here's my code: class Publisher(models.Model): name = models.CharField( max_length = 200, unique = True, ) url = models.URLField() def __unicode__(self): return self.name def save(self): pass class Item(models.Model): publisher = models.ForeignKey(Publisher) name = models.CharField( max_...


python - make a parent function return - super return?

there is a check I need to perform after each subsequent step in a function, so I wanted to define that step as a function within a function. >>> def gs(a,b): ... def ry(): ... if a==b: ... return a ... ... ry() ... ... a += 1 ... ry() ... ... b*=2 ... ry() ... >>> gs(1,2) # should return 2 >>> gs(1,1) # should return 1 >>> gs(5,3) # should return 6...






Still can't find your answer? Check out these communities...



PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python



top