Ah, that’s where the confusion is then. Since that function definition is part of, and gets bound to, the class definition
self will refer to objects created by
Franchise. So it’s referring to Franchise objects rather than
So your query about how does
Franchise access attributes from
Menu objects is perfectly valid and clarified further by @codeneutrino’s point. Put simply put, it doesn’t.
What we have are a selection of objects created from
Menu that do have
end_time attributes. For example
brunch_menu = Menu('Brunch', brunch_items, 1100, 1600)
With that information can you see how to access the relevant values?
Although class notation and objects can be a bit weird at first you may already be somewhat familiar with them. Consider lists, if you create a list object you can add new names to the same object; you can also add it as an attribute to other objects e.g. our class defined instance objects (the behaviour is similar to other assignments).
def __init__(self, a_list):
self.a_list = a_list
x = [1, 2, 3]
y = x # we can assign an additional name to same object
test_obj = Test(x) # __init__ also binds it to the new object
# just to stress the point, add another attribute-
test_obj.b_list = x
Say you now wanted to update the list at
test_obj.a_list with an additional value in that sequence, an integer 4. Our
Test class doesn’t know much about the object, but the bound object itself has references to its own attributes; we’d probably use-
Technically since we have four references to the same object
test_obj.b_list.append(4) also all do exactly the same thing but the point about attributes remains. You can treat such an attribute like a normal reference. If you want to access values from the list use it like a list, if you want values from a
Menu instance, use it like a menu object.