A Class/Object and Error Handling Question

Question 1:
Why does the orbit method have to be called but the str() special method does not? Is the answer: because it is a special method?

I think I puzzled it out. The initializer is a special method that runs automatically and if it didn’t then I would have to manually call the method to assign the local variables, which would be cumbersome.

Question 2:
When testing the code block during the Build A Planet Class llab, the raise TypeError puts up a whole traceback error message. Can that be controlled or is that what the raise keyword is supposed to do?

When manually triggering the error, it caused me quite a bit of grief before I realized that I wasn’t making an error and that it also wouldn’t pass any validation checks because of the error it was throwing.

Challenge Information:

Build a Planet Class - Build a Planet Class

GitHub Link: freeCodeCamp/curriculum/challenges/english/blocks/lab-planet-class/68e76b1ce8216249d04df670.md at main · freeCodeCamp/freeCodeCamp · GitHub

the __str__ special method is engaged every time the object is converted to a string, for example when putting it inside print or using str() aroung an instance

the traceback gives additional infos, like what exact line created the error in what file, and all the chain of that if it is accross multiple functions and files, this is always present. You can instead control the error type and error message.

for example for the traceback see the difference between these two:


1 Like

when creating classes, it says we could technically replace the first parameter"self" with another variable name, but “self” always shows up in blue in the assignments. Is this a theme the website uses, or does Python recognize “self” as something other than the self-referring variable?

I think it is part of the syntax highlighting, self is standardized so much that even the syntax highlighting recognises it

On a different but associated topic, I just finished the Classes & Objects quiz and am confused by one of the questions.

“Which of the following correctly creates a new instance of a class Person?” → Person()

I don’t understand the question. Are we creating an object from the Person class? And why doesn’t the answer utilize the familiar object = ClassName syntax? If we aren’t creating a new object, are we creating a copy of the class, in which case why would just putting Person() into the code block create a new copy of the class?

I scanned back over the theory and review but see anything referenced like the question and asking Google (which is hit or miss) is answering with how to create objects.

Nevermind, I started the budgeting app exercise and understand what it’s asking.