FAQ: Object-Oriented Programming - Introduction to Object-Oriented Programming

This community-built FAQ covers the “Introduction to Object-Oriented Programming” exercise from the lesson “Object-Oriented Programming”.

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

Learn Intermediate Python 3

FAQs on the exercise Introduction to Object-Oriented Programming

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 Language Help.

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

Need broader help or resources? Head to Language Help and Tips and Resources. If you are wanting feedback or inspiration for a project, check out Projects.

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 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!

Why can’t we use self.id = self.new_id instead of self.id = Employee.new_id. Don’t they point to the same value?

3 Likes

I think self.id is newly introduced to identify an employee id and however the Employee class is already defined will determine the new_id.

hello micro4950816268, i have got same question. why can’t we use the self.id = self.new_id instead of self.id = Employee.new_id. Since it is a class variable and we can call it either with the class name or using the self after the init method.

Explain please

Why is there ‘None’ in my outputs?

My id is 1
None
My id is 2
None

My code is

# Write your code below
class Employee:
  new_id = 1

  def __init__(self):
    self.id = Employee.new_id
    Employee.new_id += 1

  def say_id(self):
    print('My id is {}'.format(self.id))


e1 = Employee()
e2 = Employee()

print(e1.say_id())
print(e2.say_id())

Thank you.

1 Like

Hi,

I believe the None is due to the fact that the say_id method has no return sentence, i.e. it returns nothing or “None”
Try replacing print by return in the method and you’ll see how the None disappears! :slight_smile:

return "My id is {}".format(self.id)

Best

1 Like

Alternatively you can just use
e1.say_id()
e2.say_id()

Using print it tries to find a return item and since the function has no such element it prints None

1 Like