About python syntax highlighting

27 views
Skip to first unread message

H Xu

unread,
Aug 14, 2010, 7:16:06 AM8/14/10
to vim_use
Hello,

I recommend the python syntax file add highlighting
for the word "self". Thank you.

Regards,
Hong Xu
2010/8/14

Zvezdan Petkovic

unread,
Aug 15, 2010, 7:40:52 PM8/15/10
to vim...@googlegroups.com
On Aug 14, 2010, at 7:16 AM, H Xu wrote:
> I recommend the python syntax file add highlighting
> for the word "self". Thank you.

Why?

There is nothing special about the word "self".
It is *not* a Python keyword.

You could call that parameter "this" if you wanted.
Use of "self" is just a common established practice, but it is not a part of the language syntax specification.

If you prefer to have it highlighted you can always make your own customized file.

Zvezdan

H Xu

unread,
Aug 15, 2010, 9:41:07 PM8/15/10
to vim...@googlegroups.com
Hello Zvezdan,

My opinion is that syntax highlighting is not only for keyword. You may
notice that in C language "NULL" is not a keyword, it has been colored.

Regards,
Hong Xu
2010/8/16

Tony Mechelynck

unread,
Aug 15, 2010, 11:06:02 PM8/15/10
to vim...@googlegroups.com, H Xu

Do you mean I have to "#define NULL" to something myself if I want to
use it for a pointer that points nowhere in C? I would never have
guessed that.


Best regards,
Tony.
--
First Corollary of Taber's Second Law:
Machines that piss people off get murdered.
-- Pat Taber

Dominique Pellé

unread,
Aug 16, 2010, 1:36:16 AM8/16/10
to vim...@googlegroups.com

NULL is defined in C standard header files (stdlib.h and stddef.h).

'self' on the other hand is merely a coding convention in Python
so I don't think it should be highlighted by default.

-- Dominique

H Xu

unread,
Aug 16, 2010, 10:40:28 AM8/16/10
to vim...@googlegroups.com
Hello,

Syntax highlighting is aimed at convenience, not for the form. If this
is a coding convention,
then "self" should be highlighted. Or maybe it is better to add an
option to turn this on and off.

Regards,
Hong Xu
2010/8/16

Benjamin R. Haskell

unread,
Aug 16, 2010, 11:16:29 AM8/16/10
to vim...@googlegroups.com
On Mon, 16 Aug 2010, H Xu wrote:

I started replying to this thinking that 'self' shouldn't be
highlighted, since it's not "special" in any way. If anything, a
"correct" fix would be to parse out the first parameter to a method call
and highlight that. But that's not worth the effort.

The point is that in Python both of the following mean exactly the same
thing:

class foo:
def whatever(self):
self.x = y

class foo:
def whatever(bar):
bar.x = y

The problem with the "but it's not special" argument is that *everyone*
uses 'self', to the point where other syntax-highlighting tools usually
syntax-highlight it.

See: http://docs.python.org/tutorial/classes.html#random-remarks
And notice the syntax highlighting.


> Or maybe it is better to add an option to turn this on and off.

Long-term, that sounds like a reasonable compromise. (Hell, C++ syntax
has an option to highlight Java keywords.) Something like:

let g:python_self_is_special = 1
to turn 'self'-highlighting on.

(or perhaps even):

let g:python_self_not_special = 1
to turn it off.


For now, though, one (easy) option is to add it yourself. It's as
simple as putting this line:

syn keyword Keyword self

In the file: ~/.vim/after/syntax/python.vim
or vimfiles\after\syntax\python.vim in your Windows home directory.

--
Best,
Ben

AK

unread,
Aug 16, 2010, 12:11:18 PM8/16/10
to vim...@googlegroups.com

I have to agree, self is effectively universal, I've never seen a python
example using anything other than self. At the same time, it's used very
often so it makes a lot of sense to highlight it, so that when you look
through code you will focus on variable names and if you misspell self
it's easy to spot.

-ak

--
Python plugins for vim: outliner, todo list, project manager, calendar,
expenses tracker, sortable table, and more |
http://lightbird.net/pysuite/

Michael Dunn

unread,
Aug 16, 2010, 1:19:22 PM8/16/10
to vim...@googlegroups.com
>>>>> I recommend the python syntax file add highlighting
>>>>> for the word "self". Thank you.

> For now, though, one (easy) option is to add it yourself.  It's as


> simple as putting this line:
>
> syn keyword Keyword self
>
> In the file: ~/.vim/after/syntax/python.vim
> or vimfiles\after\syntax\python.vim in your Windows home directory.

This is definitely the best solution! It's precisely what the
.vim/after/ directory is for...

Tony Mechelynck

unread,
Aug 21, 2010, 11:22:59 AM8/21/10
to vim...@googlegroups.com
On 16/08/10 17:16, Benjamin R. Haskell wrote:
> On Mon, 16 Aug 2010, H Xu wrote:
>

or, to be finicky, maybe use one of the highlight groups starting with
python defined in the syntax/python.vim script, maybe

:syn keyword pythonBuiltin self

(pretending that "self", like "False" "True" and "None", is a reserved
word?)


Best regards,
Tony.
--
INSPECTOR END OF FILM: Move along. There's nothing to see! Keep moving!
[Suddenly he notices the cameras.]
INSPECTOR END OF FILM: (to Camera) All right, put that away sonny.
[He walks over to it and puts his hand over the lens.]
"Monty Python and the Holy Grail" PYTHON (MONTY)
PICTURES LTD

Reply all
Reply to author
Forward
0 new messages