Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion implement-cowsay/cow.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ def main():
try:
animal_func = getattr(cowsay, args.animal)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can check out an alternative get_output_string method here to avoid getattr (not the best choice to use if there is alternative - it make code more readable).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noted, Thank you 🙏

output = animal_func(message)
print(output)
if output is not None:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So can you explain in which cases the output is None here and when it is not None?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Output is None only if the animal function doesn't return anything. In the official cowsay module, it always returns a string.

The output is not None in normal cases because standard cowsay animal functions return a string of ASCII art.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does it print None when you remove if output is not None:? What is considered to be not a normal case?

print(output)
except AttributeError:
print(f"Error: '{args.animal}' is not a valid animal.", file=sys.stderr)
sys.exit(1)
Expand Down