0

I've been messing around with python a little bit and have created a program saved as string.py where I tested out some string functions. In another program function.py in the same directory I wrote this code

def say_hi(first = 'John', last = 'Doe'):
    """Say hello."""
    print('Hi {} {}!'.format(first, last))

help(say_hi)

which, however, executed the string.py program. I found out after some testing that renaming string.py to anything else solves the problem and the function.py program is executed as intended, but I'd like to understand why the help function executed the other program in the first place.

hhblackno
  • 17
  • 3
  • What does it mean *"however, executed the string.py program"*? Putting your code in a file and running it returns the expected output (the help message of the above function) – Tomerikoo Feb 15 '22 at 17:47
  • 1
    So if you run `python3 function.py`, you're saying that `string.py` is run? I don't see how that's possible. Are you using an IDE? If so it could still have its build/run target be `string.py` even while you're editing `function.py`, so you need to explicitly run `function.py`. Without more info though or a [mre] we can only take wild guesses as to what your issue is. – Random Davis Feb 15 '22 at 17:48
  • 2
    `help()` is apparently importing the standard `string` module, and getting your file instead. Python's module search path normally has the current directory listed in front of the standard module directories. – jasonharper Feb 15 '22 at 17:49
  • @jasonharper I even created a `string.py` as well and didn't have such issue (Python 3.9)... I believe it must be something like Random Davis mentioned... – Tomerikoo Feb 15 '22 at 17:50
  • @RandomDavis I wrote the program in SublimeText and executed it in Windows PowerShell with `py function.py`. When I moved `string.py` or `function.py` to a different directory, or kept them in the same directory but renamed `string.py`, the problem resolved itself. – hhblackno Feb 15 '22 at 17:56
  • What version of Python are you using? Is that the full code of `function.py`? There are no imports? – Tomerikoo Feb 15 '22 at 17:57
  • @Tomerikoo Python 3.10.0, and no, no imports. I called the say_hi function a few times after that code snippet but commenting that out didn't change anything. – hhblackno Feb 15 '22 at 18:03
  • 1
    Sorry can't reproduce this both Python 3.9 and 3.10. Created a file with the same code as above, created a `string.py` in the same directory - no problems... – Tomerikoo Feb 15 '22 at 18:17

1 Answers1

3

You are shadowing the name string, which is a built-in module: https://docs.python.org/3/library/string.html

string is a commonly used module. Lots of built-in functions in python run scripts that have import string in them, meaning it'll import your string.py and not the built-in one.

This is just yet another example of why it's a bad idea to name scripts or variables with names that already exist in standard Python.

Random Davis
  • 6,662
  • 4
  • 14
  • 24
  • That is all well and good. So how come reproducing the same scenario doesn't give the same outcome? I get the expected help message even though there is a `string.py` file in the same directory... Did you also try it and the same happened? – Tomerikoo Feb 15 '22 at 18:16
  • @Tomerikoo I don't know what you're doing differently because I'm able to reproduce the issue easily. I put 2 files in a directory; `string.py` (which contains nothing but a print statement), and another script file which contains the example code in this question. When I ran it I got `AttributeError: module 'string' has no attribute 'ascii_letters'`. That happened regardless of whether or not I had an `__init__.py` file in the directory. – Random Davis Feb 15 '22 at 18:29