1

Suppose I have a Python module my_class.py (see PEP 8 convention for modules) that holds a class MyClass (see PEP 8 convention for classes).

Now I want to instantiate that class in a module in the same package and would normally do

from .my_class import MyClass
my_class = MyClass()

since PEP 8 proposes this naming scheme for instance variable names here.

However, in this context, my_class already refers to the module my_class.py and the instance variable would therefore shadow the module.

Do PEP 8 or any other sources give any direction on how to proceed here, or is it just up to the developer to think of a different but less intuitive name for the variable?

2
  • 2
    Since instantiated class represents an instance (rather than "just a class"), the appropriate thing to write would be my_instance = MyClass(), wouldn't it? Commented Jul 16, 2019 at 7:11
  • @Smuuf calling it MyClass is just an example, in a real useacase the class might not be called MyClass but e. g. JsonParser. Commented Jul 16, 2019 at 9:01

1 Answer 1

4

In your example, you have explicitly imported MyClass.

While the module my_class is imported it will not be assigned a variable in your local scope and hence you will not be shadowing it by using my_class for your MyClass instance.

Update

When importing a module within the same package it looks like a reference is added to the local scope, however, there is no risk in using the same name. Variables in Python are references to an object rather than the object.

When you reassign a reference (eg use the name for another object) the object is deleted if there are no other references to the object. In the case of modules they are added to sys.modules when imported, in which case there is always another reference and they are not deleted.

Sign up to request clarification or add additional context in comments.

9 Comments

Thank you! Why is it then that I still don't get an error if I do a = my_class (without doing my_class = MyClass() aka without defining a local variable called my_class? In my understanding, this is because my_class is a reference to the module that would be shadowed if I defined a variable of the same name... Or am I mistaken?
@TobiasFeil You will get an error if you simply do from .my_class import MyClass; a = my_class.
Also, use print(my_class) to see what it holds. E.g. for me print(os) (after importing it) is <module 'os' from '/usr/lib/python3.6/os.py'>
@L3viathan Sorry, I should have said that I'm doing so in my package's __init__.py, because I'm not getting an error there.
Looking closer at this it's likely related to how modules within the same package are imported. However, it looks like a side effect and I would not rely on this behaviour. Unless you are explicitly importing an item (or the name is a builtin) you can safely use the name for a variable, there is no risk in doing so.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.