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 trialAlexa Kapor-Mater
4,674 PointsWhy is self.sides not specified as an attribute in Die?
I'm puzzled about why self.sides isn't specified as an attribute in the Die class? Is this an oversight in the video, or is there a specific reason not to do this? Thanks!
1 Answer
jonlunsford
16,746 PointsIn the video, "sides" is passed as an argument, but it is not stored as an attribute of the Class. You can check this by running the following code:
d = Die()
d.__dict__
{'value':1}
As you see above, only value is an attribute of the object "d". In the video, "sides" is used to limit the value returned in the random function, and there is no particular reason to store it's value. If sides were an attribute of the class, then the code to create the attribute would have been included.
self.sides = sides
In Python, "dot" notation is used to create attributes. With Python objects, you don't have to set attributes in the Class to use them on the object. If they are set in the Class then all instances of the class will share that attribute. I know this is confusing, but you could just as well set a color attribute after the Die() object was instantiated, such as:
d = Die()
d.color = "blue"
In this example, the color attribute is not part of the Class, just the instance of the class called "d".
To answer your question, sides could be added to the Class, if there were a need to store the sides for future use. In this case, it is completely up to the developer how to structure the Class.
Hope this helps!