2

I'm using vim 7.3, scripting in python 2.7. I have some syntax highlighting, but mostly just for number, strings, comments, and a few core functions and keywords. I have tried the core vim python.vim syntax file, and the one from vim.org

Is there any way to get (separate colour) highlighting for variables and function names?

1 Answer 1

6

There is not really a difference between variables and functions in python (both are first-class objects in python). So that's pretty much impossible without actually running the code and testing if callable(var) is true.

And there are always cases where such a behaviour would be confusing:

class Dummy(object):
    pass
foo = Dummy()
if False:
    foo()
foo.__call__ = lambda self: 'meow'
foo.x = 'y'
foo()

When would you highlight foo as a function now? It cannot be called until after the __call__ assignment but having different syntax highlighting for the same object would be pretty confusing. Of course the example is rather stupid. But it shows easily why it's not really possible to do what you want in python. You could make it even more complicated by using inheritance and metaclasses.

Sign up to request clarification or add additional context in comments.

5 Comments

+1. Well, but the main reason for syntax highlighting is a better orientation in the code. naught101 may want to emphasize the function name in the definition and when the function is called (i.e. when parentheses follow). The question is if it is a good idea to pee against the wind ;)
Point taken. Good answer, as far as the misguided question allows. I wonder then if it'd be worth highlighting functions and variables in the same colour, or if that'd more or less highlight everything?
Maybe a middle ground would be to highlight locally defined variables. That would distinguish between functions and variables in most cases.
As someone relatively new to python, this lack of differentiation makes reading python much harder :-/ Combine that with the approach to documentation ("""put this after your def:""") which breaks off the method definitions from their implementation and the significant whitespace (no braces to help orient yourself) and I'm finding that python is really not read friendly at all. Hopefully this is just an acclimatisation thing and I'll get used to it, but it does make it harder to pick up.
Classes are supposed to use CamelCase naming style while variables, functions, etc use snake_case. So if you follow these guidelines, you immediately know if something is (most likely) a class. Anyway, you will get used to it quickly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.