Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Basic Object-Oriented Python Creating a Memory Game Card Class

Couple of questions

Hey,

so a couple of things I wanted to ask, which i have probably missed so far! Please lend me a helping hand!

  1. The init function builds the attributes of the class, right? Does it do something else other than defining attributes? If we don't use init the other functions would not work, right?

  2. Why don't we use self.location on any of the other functions?

  3. Why don't we use f'{self.card} on the str function but just return self.card?

  4. Why does the dunder main method(?) have these '' ('main') and all the other dunder methods(?) don't (eg. str or name?)

Thank you alot, and I apologize in advance if some of the questions are too basic :)

Have a good day!

2 Answers

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 28,720 Points

You ask some very substantial questions -- it shows you are really thinking about what object orientation implies. That's very cool.

1) __init__ is an initializer or constructor for a particular class. It is called when a class is instantiated (or is created).

Another custom method of a class can be defined to do the other work of the class, and yes it can initialize/change properties too.

Python can be written with an object-oriented focus, but can also be written "functional programming" style. It's totally flexible.

2) Lacy is using self.location very specifically. You could write 'setters' and getters' (specific Python methods to set properties if you wanted). But it makes sense to manipulate those directly (when you change a location, for example).

3) You have cleverly identified a best practice for object types! Lacy is keeping it simple for this example.

4) I think you are identifying that a Python program can be an object too. The program does have dunder properties and methods, but these are fairly limited. It is a special case at the top-level of a program. You don't have self to reference. Later on, you'll make more use of these.

Take a look at this concise resource it should come in handy: https://www.geeksforgeeks.org/dunder-magic-methods-python

Have a great Python journey, you've got quite a bit of natural curiosity which will serve you well!

Thank you very much for the friendly reply!