-1

Whenever I use id as my variable name, my IDE shows the term in different colour from other variables. Is this expected or is it some feature of IDE (I'm using vs code) or shouldn't i use id as a variable?

I haven't faced any issues while running code. Only the colour change makes me curious.

Example Image from my IDE: Example Image

ilyasbbu
  • 1,468
  • 4
  • 11
  • 22
  • 3
    This is because `id` is an builtin function – Abdul Niyas P M Nov 01 '22 at 09:47
  • 3
    It is a bad practice few keywords a reserve for python builtin methods, when you can use it then it can create a mess. – Mehmaam Nov 01 '22 at 09:47
  • technically its possible but it's very bad practice because id is builtin. – George Imerlishvili Nov 01 '22 at 09:55
  • 4
    in a class this is fine, it doesn't shadow anything – juanpa.arrivillaga Nov 01 '22 at 10:05
  • Not a dupe. This is fine; the answers are (IMHO) wrong. Having an attribute on a class shadow a builtin is *not* the same as shadowing a builtin in a function, and *not* the same as shadowing in global/module scope. `id` only shadows *outside* the methods. I highly doubt you shold be calling `id` in the definition of a class anyhow. (NB I'm assuming this is a dataclass). – 2e0byo Nov 01 '22 at 10:09
  • However since this is dupe-hammered I can't post an answer, so answering in the comments... – 2e0byo Nov 01 '22 at 10:09
  • @2e0byo logically correct...Idk why i've tried python IDE, VScode, Jupyter notebook, Spider...They are highliting `id` inside the `class` – Bhargav - Retarded Skills Nov 01 '22 at 10:12
  • 3
    Even shadowing `id` in a function might well be alright: in some logic `id` can only ever mean something *other* than `id()` (e.g. a db lookup for ephemeral objects). Whether such a (small) function would pass code review is a matter for local styles, but there are arguments both ways. Lots of code out there still uses `match` for `re.Match()` objects, even though that has now become syntax. Python worked to make that work (by ignoring `match` outside pattern matches). But all/most of the IDES will highlight these `match`s, despite them *not* being syntax in these instances. – 2e0byo Nov 01 '22 at 10:12
  • 1
    @Bhargav until `id` is defined it's the `id()` builtin. The ide probably just uses a glorified regex anyhow, but `Foo.id` is *never* going to be `id` unless you bind it explicitly. Yeah, it does stop you calling `id()` at eval time in the outer class scope, but that's a pretty big code smell already. If the OP isn't using something like `@dataclass` there's a bigger problem with this class, since `id` clearly belongs on the instance. – 2e0byo Nov 01 '22 at 10:16
  • Wow what a keen observation!..Never thought of that! – Bhargav - Retarded Skills Nov 01 '22 at 10:16
  • 1
    The point about `match` btw was just that Python is pretty averse to breaking existing code, and it's extremely unlikely to forbid shadowing at any point in the future. You shoudn't do it willy-nilly, but there are definitely cases (like class attributes) where it can make sense. – 2e0byo Nov 01 '22 at 10:18
  • Feel free to edit my answer @2e0byo..it may be helpful to others ..a lot of information here!! – Bhargav - Retarded Skills Nov 01 '22 at 10:24
  • 1
    I couldn't find a canonical dupe for this, so I've created one [here](https://stackoverflow.com/questions/74274877/when-is-it-acceptable-to-shadow-a-builtin/74274878#74274878). The answer is a community wiki. – 2e0byo Nov 01 '22 at 11:00
  • ... and the canonical I made has since been closed as opinion based. Oh well. – 2e0byo Nov 01 '22 at 15:49
  • @ilyasbbu what is that, it looks real nice. – se7en Nov 16 '22 at 10:50
  • @se7en What do you mean? – ilyasbbu Nov 16 '22 at 11:59
  • @ilyasbbu sorry, I mean what Theme is that. I want to download it – se7en Nov 16 '22 at 12:45
  • 1
    @se7en it is [noctis-high-contrast](https://marketplace.visualstudio.com/items?itemName=Kamen.noctis-high-contrast) – ilyasbbu Nov 17 '22 at 09:54

2 Answers2

3

Shorter Answer:

You can use id as a variable name, but should not. That being said, in the example you gave, you are using id as a class' attribute (not a variable) which is fine to do.

Longer Answers:

As noted by other answers, id is a builtin function, so naming a variable id is not ideal as it obfuscates the underlying function. There is a question with more detail about why this can cause problems here.

However, the example in the screenshot you provided is not using id as a variable, but as a class' attribute (you can read more about classes and their attributes in the Python docs on the subject). This is fine as there is no builtin attribute named id (credit to "juanpa.arrivillaga" for first noting this in a comment on a different answer). The fact that it is highlighted differently is likely an error in your IDE/text editor's syntax parsing.

Floyd
  • 2,252
  • 19
  • 25
0

In Python3, id is Built-in Functions.

refs: https://docs.python.org/3/library/functions.html#id