Consider this python code
``` class example: def __init__(self): self.hello = "Hello" def world(self): return "World !"
e = example() print(e.hello) print(e.world()) ``` I am using a Tango color scheme and currently all of these
``` e = example() e.hello e.world() ``` have the same color (ie white) in geany. It would be nice if we get different colors for objects,methods and attributes.
Unfortunately (from the point of view of highlighting) with a totally dynamic language like Python you must effectively execute the code to find the type of a name. And without knowing that any expression based on the name cannot be known.
In your example, `example`, `__init__`, and `world` are declarations, that is syntax that explicitly defines a name and what type it is (`example` is a class, and `__init__` and `world` are functions and the highlighter identifies them as such).
But in `e=example()` there is no syntax to say `e` is being declared, it is an assignment expression which would have to be evaluated, or at least type inferred, to decide that `e` is being declared here, and evaluation and type inference is beyond the pay grade of simple syntax highlighters.
Even `example()` is no guarantee that it is a class instantiation, since Python is a dynamic language the name can be redefined between the class declaration and the use of `example()` so as I said evaluation or complex type inference would be needed to see what `example` is bound to at any point in the code. And also the type of a name may be different depending on which branch of an `if` the code takes, eg:
``` if foo: blah=example() else: blah=other() blah.x ``` So what is `blah` and `blah.x`, depends on `foo` and that might be the result of a data read, but at least it involves evaluating the code that sets `foo`.
AFAICT even IDEs like Eclipse or Python's own Idle don't do any better with highlighting.
Static languages do better with highlighting because all names are declared, and the type of names can't be changed, although it still isn't perfect due to scope issues.
It would be possible to have separate lexer styles for function calls, distinguished when they are part of an attribute access expression, as well as field/property accessors. But for the reasons @elextr mentioned, there's no way to really be sure (semantically) what those things are/refer to, it would literally just be based on the tokens in the file and looking for a sequence like `IDENT DOT IDENT LPAREN arguments_list* RPAREN` or whatever.
That said, any work on the lexer/highlighting needs to first be done in [Scintilla](https://www.scintilla.org/) (where the lexer comes from), and should be opened as a [feature request](https://sourceforge.net/p/scintilla/feature-requests/) there, assuming none such exists. For this reason combined with what @elextr said, I'm going to close this Issue.
Closed #2441.
a sequence like IDENT DOT IDENT LPAREN arguments_list* RPAREN or whatever.
Whilst thats usually a function call Python won't even give you that :cry:, it still could be a class instantiation since Python uses DOT for namespace access, eg imports of packages.
github-comments@lists.geany.org