0

example:

# Suppose id(i) == A
i = 10 

if some_condition:
  i = 11
  # How to make sure this 'i' has an id == A instead of B?
  # Because at this time my pycharm prompts me that the i above is not used.

And what should I do when I want to declare a local variable just with the same name without affecting the value of i above?

Or, is there a way to force only assignment without declaring variables in python3, then an exception thrown to prompt developer when the name doesn't exist?

I will be appreciated if anyone offers help.

  • Besides, `nonlocal` and `global` can’t meet this requirement (at least not very convenient). – Isuxiz Slidder Nov 01 '21 at 06:56
  • What is your _actual_ use case here (not in terms of `i` and `some_condition`)? It sounds like you're trying to contort Python to doing something unusual. – AKX Nov 01 '21 at 06:58
  • 1
    That's not how Python works - you're better off learning how Python works, as it makes perfect sense by itself, instead of trying to make it work like you're used to from other languages. Inside a function, you can redefine variables with the same name, but it's not recommended if those names are actually defined globally for a reason (it's called shadowing). In an `if .. else ..`-block, you can't (and shouldn't) just redefine variables. If you feel the need to, you probably have another problem that should be solved in a better way. – Grismar Nov 01 '21 at 07:00
  • 2
    You might misunderstand `id`. It is giving a unique identifier for the object, not the name, so it is natural that it will change when you reassign `i`. pycharm is just telling you that it thinks `i = 10` was pointless because it is never used. – Passerby Nov 01 '21 at 07:01
  • In Python if-statement or any other logical blocks don’t have scope. Also `id(variable)` is the address of the _value_ not the variable - if I were you i’d forget about `id()` because it’s an irrelevance. Read about Python scopes in the documentation https://docs.python.org/3/tutorial/classes.html?highlight=scope#python-scopes-and-namespaces – DisappointedByUnaccountableMod Nov 01 '21 at 07:44
  • Your question seems to be driven by two misconceptions: Scope in Python is per function not block. The ID is per object not variable. Basically your code already does what you expect it to do, you just don't realize it because the tool you use to check this does not what you expect it to do. – MisterMiyagi Nov 01 '21 at 08:41
  • Functions and classes and modules have scope. Technically comprehensions have scope too. I often wish Python had an explicit 'local' variable scope (so I could define/use a variable in that scope and it's automatically fogotten after this local scope) but there isn't one. – DisappointedByUnaccountableMod Nov 01 '21 at 08:44
  • Does this answer your question? [Short description of the scoping rules?](https://stackoverflow.com/questions/291978/short-description-of-the-scoping-rules) – MisterMiyagi Nov 01 '21 at 09:33
  • @Grismar Sorry for taking so long to reply, thanks for your comment. I will ask this weird question because of the scope of curly braces in C. Maybe I do need some style changes to make my codes more pythonic. – Isuxiz Slidder Nov 09 '21 at 14:19
  • @balmy Sorry for taking so long to reply, thanks for your comment. I do should read a few docs. The constant switching between C/CPP, Javascript and python gave me a lot of strange ideas, and I felt a little dizzy. – Isuxiz Slidder Nov 09 '21 at 14:21
  • @MisterMiyagi Sorry for taking so long to reply, thanks for your comment. You are right, after some experiments i found i have done what i expected. But pycharm still give me a prompt, is there any way to correct this behavior? Thanks again. – Isuxiz Slidder Nov 09 '21 at 14:25

1 Answers1

0

i think, the best answer is change the variable. But , if u still want to use it. U can try this

Make some function

i = 10
def scope_test():
    def do_local():
        i = 11

    do_local()
    print(i)

print(i)

scope_test()
print(i)
renaldyks
  • 188
  • 1
  • 15