Python shadows built in name id

Shadows built-in names «function» and «module» with PyCharm

Still, if for any reason you absolutely need to reuse variable names and want to get rid of the warning message, you can disable it in Pycharm (Preferences -> Editor -> Code Style -> Inspections -> shadowing names from outer scopes). Solution 3: That warning is not a PEP8 problem, but an actual warning from Pycharm to tell you that reusing variable names in that way is a bad idea.

Shadows built-in names «function» and «module» with PyCharm

I have the following Python code:

function = "Developer" module = "something" print(function + " on " + module) 

With PyCharm 2017, I have a bubble which says “Shadows built-in names «function»/»module» with PyCharm”.

I’m surprised because «function» and «module» are not built-in names. They are not keywords either:

import __builtin__ import keyword assert "function" not in dir(__builtin__) # -> OK assert "module" not in dir(__builtin__) # -> OK assert "function" not in keyword.kwlist # -> OK assert "module" not in keyword.kwlist # -> OK 

I’m using CPython 2.7, but have the same trouble with 3.5 & 3.6.

__builtin__ is now builtins in Python 3.

function is «defined» in builtins.pyi :

class function: # TODO not defined in builtins! __name__ = . # type: str __qualname__ = . # type: str __module__ = . # type: str __code__ = . # type: Any __annotations__ = . # type: Dict[str, Any] 

Keep in mind I used «defined» vs defined. Check out this absurdity:

Traceback (most recent call last): File "main.py", line 117, in foo = function NameError: name 'function' is not defined 

Yet if you do function = ‘a’ the IDE will complain (as you noticed) that this shadows a built-in name (even though function is clearly not actually defined).

The exact behavior repeats with module .

This is because (as far as I understand, anyone please correct me if I’m wrong) pyi files are only there to provide type hints (as PEP-484 suggests).

So, I’m not sure if this warning is a bug in Pycharm’s linter (perhaps it should not be looking at «definitions» in .pyi files) or an intended behavior.

Either way, module and function are probably not good variable names anyway.

Per PY-8672, since March 2014 there is an ability to ignore certain names with this inspection. Open Settings, search «Shadowing built-ins», click on the inspection name, and use the Options section to add names the inspection should whitelist.

Shadows built-in names «function» and «module» with, Open Settings, search «Shadowing built-ins», click on the inspection name, and use the Options section to add names the inspection should whitelist. Share Improve this answer answered Dec 6, 2017 at 17:36 theY4Kman 4,982 2 29 37 Add a comment Code sampleTraceback (most recent call last):File «main.py», line 117, in foo = functionNameError: name ‘function’ is not definedFeedback

Читайте также:  Python найти максимум трех чисел

HOW TO CREATE A SHADOW AROUND TEXT IN CRICUT

Are you wanting to make a shadow around your text? In this quick tutorial I will show you how easy it is to create a shadow offset outline for Cricut Design

I made a new clan named urban shadows

About Press Copyright Contact us Creators Advertise Developers Terms Privacy Policy & Safety How YouTube works Test new features Press Copyright Contact us …

PyCharm: Shadows built in name ‘self’, bug or feature?

I’m using PyCharm IDE; I love it for all the work it does for me. I found it very powerful in my process of learning python. It has all the hints about variables names, typos etc.

However now I’m a little bit confused. When using PyCharm with IronPython 2.7, Pycharm is throwing me an info while class methods definition.

Shadows built-in name 'self' 

In my opinion everything is ok with that class:

class A(object): """ Cheers Stackoverflowers """ def __init__(self, x): self.x = x + 2 def calculate_something(self, y): self.x = y * 2 

But I would like to be 100% correct while programming. And if there are more correct ways to define method in class definition I would like to know where I can find information about that.

Pycharm screenshot showing error

It does not happen while using CPython

PyCharm: Shadows built in name ‘self’, bug or feature?, Shadows built-in name ‘self’. In my opinion everything is ok with that class: class A (object): «»» Cheers Stackoverflowers «»» def __init__ (self, x): self.x = x + 2 def calculate_something (self, y): self.x = y * 2. But I would like to be 100% correct while programming. And if there are more correct ways to define method …

Is it bad practice to use a built-in function name as an attribute or method identifier?

I know to never use built-in function names as variable identifiers.

But are there any reasons not to use them as attribute or method identifiers?

For example, is it safe to write my_object.id = 5 , or define an instance method dict in my own class?

It won’t confuse the interpreter but it may confuse people reading your code. Unnecessary use of builtin names for attributes and methods should be avoided.

Another ill-effect is that shadowing builtins confuses syntax highlighters in most python-aware editors (vi, emacs, pydev, idle, etc.) Also, some of the lint tools will warn about this practice.

Yes it’s bad practice. It might not immediately break anything for you, but it still hurts readability of the code.

To selectively quote from PEP20:

Beautiful is better than ugly.
Simple is better than complex.
Readability counts.
If the implementation is hard to explain, it’s a bad idea.

Seeing a call to myobject.dict() it would be natural to assume that it’s going to return myobject.__dict__ , or that myobject.id() returns the same thing as id(myobject)

Читайте также:  Диапазон ввода чисел питон

It’s possible for them to find out that they’re wrong; but that will take time and effort and probably lead to some mistakes while they figure it out. Calling your attribute myobject.object_id_number is much longer, but makes it clearer that it’s different to id(myobject)

No, that’s fine. Since an object reference is required there is no way to have them shadow the built-in.

I go back and forth on functions a lot when the input variables mimic python builtins. For example, the word bytes is a python builtin, but consider a utility library that parses bytes:

def parse_bytes(bytes): pass 

I’d argue this has great readability , but pep8 linters don’t like it. Instead I could do

def parse_bytes(bytearray): pass def parse_bytes(somebytes): pass 
def parse_bytes(b: bytes): pass 

But all of these seem worse. Same thing happens if your variable name is input .

At the end of the day I usually go with somebytes

What is the problem with shadowing names defined in, Finally, built-in functions and types also live in the same namespace and can be shadowed the same way. None of this is much of a problem if you have short functions, good naming and a decent unit test coverage, but well, sometimes you have to maintain less than perfect code and being warned about such possible …

How to name variable correctly to avoid warning like «Shadows name from outer scope» in Python

I use PyCharm for my python program, and I wrote codes below:

def get_files_name(): root_dir = "/Volumes/NO NAME/" for root, ds, fs in os.walk(root_dir): for f in fs: print(os.path.join(root_dir, f)) get_files_name() for root, ds, fs in os.walk(other_dir): pass 

So I get a Warning text like «Shadows name ‘ds’ from outer scope». I know the effect of scope, but I still want to use the same code format like «for root, ds, fs in . » at inner or outer of scope.

I have searched PEP8, however, I still don’t know how to name variable in function normatively.

Could you give me some suggestion?

In general: just ignore the warning. It’s just a warning, not an error. You have use for both global and local names that happen to match.

However, I’d not put an os.walk() call at global scope anyway . I’d rather put that into a function too, which has the happy side-effect of the names you used no longer being globals.

For example, you could use a main() function:

def main(): get_files_name() for root, ds, fs in os.walk(other_dir): pass if __name__ == '__main__': main() 

Generally speaking, you don’t want to leave loop names like root, ds, fs as globals in your module anyway. Those are implementation details, and should not become part of the public API of a module. If you have to use a for loop like that at the global scope, use _ single-underscore prefixes on the names and consider deleting them after the loop with del :

for _root, _ds, _fs in os.walk(other_dir): # do something with the files or directories # clean variables for the loop that are not part of the API del _root, _ds, _fs 

If your names repeats use «_» to avoid such warnings.It’s a common practice.

def get_files_name(): root_dir = "/Volumes/NO NAME/" for root, _ds, fs in os.walk(root_dir): for f in fs: print(os.path.join(root_dir, f)) get_files_name() for root, _ds, fs in os.walk(other_dir): pass 

That warning shadows name XX from outer scope is not a PEP8 problem, but an actual warning from Pycharm to tell you that reusing variable names in that way is a bad idea. In other words, this is not a code style problem, but something that may bring problems later on in larger programs.

Читайте также:  Repeated strings in php

My suggestion would be to, well, avoid reusing variable names when possible. Typing this:

for root_path, directory_name, file_name in os.walk(root_dir):

doesn’t take a lot of time, and would avoid undesirable side-effects in the future.

Still, if for any reason you absolutely need to reuse variable names and want to get rid of the warning message, you can disable it in Pycharm (Preferences -> Editor -> Code Style -> Inspections -> shadowing names from outer scopes). But this is usually a bad idea.

Python — Is it bad practice to use a built-in function name, Unnecessary use of builtin names for attributes and methods should be avoided. Another ill-effect is that shadowing builtins confuses syntax highlighters in most python-aware editors (vi, emacs, pydev, idle, etc.)

Источник

Python – Shadows built-in names “function” and “module” with PyCharm

With PyCharm 2017, I have a bubble which says “Shadows built-in names «function»/»module» with PyCharm”.

I’m surprised because «function» and «module» are not built-in names. They are not keywords either:

import __builtin__ import keyword assert "function" not in dir(__builtin__) # -> OK assert "module" not in dir(__builtin__) # -> OK assert "function" not in keyword.kwlist # -> OK assert "module" not in keyword.kwlist # -> OK 

I’m using CPython 2.7, but have the same trouble with 3.5 & 3.6.

__builtin__ is now builtins in Python 3.

Best Solution

function is «defined» in builtins.pyi :

class function: # TODO not defined in builtins! __name__ = . # type: str __qualname__ = . # type: str __module__ = . # type: str __code__ = . # type: Any __annotations__ = . # type: Dict[str, Any] 

Keep in mind I used «defined» vs defined. Check out this absurdity:

Traceback (most recent call last): File "main.py", line 117, in foo = function NameError: name 'function' is not defined 

Yet if you do function = ‘a’ the IDE will complain (as you noticed) that this shadows a built-in name (even though function is clearly not actually defined).

The exact behavior repeats with module .

This is because (as far as I understand, anyone please correct me if I’m wrong) pyi files are only there to provide type hints (as PEP-484 suggests).

So, I’m not sure if this warning is a bug in Pycharm’s linter (perhaps it should not be looking at «definitions» in .pyi files) or an intended behavior.

Either way, module and function are probably not good variable names anyway.

Источник

Оцените статью