Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: 'dict' object is not callable #243

Open
SDAndrews opened this issue Mar 12, 2023 · 1 comment
Open

TypeError: 'dict' object is not callable #243

SDAndrews opened this issue Mar 12, 2023 · 1 comment

Comments

@SDAndrews
Copy link

Hi team,

Hope everyone is well.

Warning before I begin: novice coder here.

I'm using the pyperclip module as part of @asweigart's amazing 'Automate the...' book and am currently in chapter 6: manipulating strings. One of the chapter projects focuses on Multi-Clipboard Automatic Messages. In this project Al's code (below) is left in a file called mclip.py but called via batch file and the Windows-R short cut. A sample command is "mclip busy." I've got all the components working together but I keep generating an error message that states "TypeError: 'dict' object is not callable"

As far as I can understand the command line is objecting to this line "pyperclip.copy(TEXT(keyphrase)) " but I'm unclear on how to fix it. Any ideas would be appreciated.

`#! python3

mclip.py - A multi-clipboard program.

TEXT = {'agree': """Yes, I agree. That sounds fine to me.""",
'busy': """Sorry, can we do this later this week or next week?""",
'upsell': """Would you consider making this a monthly donation?""",
'Absence': """ I'm not in the office that day """}

import sys, pyperclip
if len(sys.argv) < 2:
print('Usage: python mclip.py [keyphrase] - copy key text')
sys.exit()

keyphrase = sys.argv[1] # first command line arg is the keyphrase

if keyphrase in TEXT:
pyperclip.copy(TEXT(keyphrase))
print('Text for ' + keyphrase + ' copied to the clipboard')
else:
print('There is no text for ' + keyphrase)`

@timbr0wn
Copy link

Hi Steven! The problem is caused by TEXT(keyphrase), because TEXT is a dictionary, but you are attempting to call it like a function (hence the "TypeError: 'dict' object is not callable").

If you are sure that the key exists in your dictionary, like you are inside the if block, then you can use square brackets [] to get the value associated with the dictionary key. In your scenario, that would be TEXT[keyphrase]

Alternatively, you could also do TEXT.get(keyphrase), which will return None if the key is not found instead of raising an error. You can also set your own default value instead of None. For example, you could do TEXT.get(keyphrase, "my fallback value")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants