FAQ: Exceptions - Customizing User-defined Exceptions

This community-built FAQ covers the “Customizing User-defined Exceptions” exercise from the lesson “Exceptions”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Intermediate Python 3

FAQs on the exercise Customizing User-defined Exceptions

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in #get-help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.

Looking for motivation to keep learning? Join our wider discussions in #community

Learn more about how to use this guide.

Found a bug? Report it online, or post in #community:Codecademy-Bug-Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Some parts of this course marks you wrong if you use f strings instead of string concatenation or .format()(someone, @mtf).

1 Like

The f-string is relatively new, and much of the course material was written before it was introduced. Guess the only thing we can do is try, and if it doesn’t work (as in pass) then revert to the legacy methods.

It worked in older courses like the Learn Python 3 Course. This course is not old; it’s actually new.

TBH, I have no idea how old or new half the courses are. We only get the flack on this side, and have no part in creating courses. With so many writers coming and going, and very little quality control, it’s no wonder the SCTs for each course differ by so much. Nothing we can do but pass on the message.

Hi! Could we use repr() method instead of str() in first excample? And If not, why? repr() return string, we need string :slight_smile: Or do I miss something?

If the intention is to give the human user something that is meaningful to them, such a date or string value then stick with __str__().

Say you have an attribute that is assigned a datetime object. If __repr__() is used, it will be the object representation that the human reader will see, which won’t make a lot of sense. However, using __str__() the user will see the actual date string…

2021-11-28 11:55:25.2345678
1 Like

Thanks a lot. I got it

1 Like

Here’s a simple example…

>>> from datetime import datetime
>>> class Departure:
	def __init__(self, city):
		self.city = city
		self.date = datetime.now()
	def __str__(self):
		return self.date

	
>>> yeg = Departure('Edmonton')
>>> print (yeg.date)
2021-11-28 12:27:08.369722    # string representation
>>> yeg.date
datetime.datetime(2021, 11, 28, 12, 27, 8, 369722) # object representation
>>> 
1 Like