boB Stepp
2015-08-14 04:31:17 UTC
I was looking at an example illustrating composition from the book,
def __init__(self, description):
self.description = description
def __init__(self, length):
self.length = length
def __init__(self, bill, tail):
self.bill = bill
self.tail = tail
def about(self):
print('This duck has a', bill.description, 'bill and a',
tail.length, 'tail.')
Here I was mildly surprised that bill and tail were not Bill and Tail,
and in the about method that self.bill was not used in place of
bill.description, etc.
From this I am forced to conclude that composition will only work with
particular instances of objects and not with any old objects created
from their respective classes. Is this understanding correct?
Thanks!
def __init__(self, description):
self.description = description
def __init__(self, length):
self.length = length
def __init__(self, bill, tail):
self.bill = bill
self.tail = tail
def about(self):
print('This duck has a', bill.description, 'bill and a',
tail.length, 'tail.')
Here I was mildly surprised that bill and tail were not Bill and Tail,
and in the about method that self.bill was not used in place of
bill.description, etc.
tail = Tail('long')
bill = Bill('wide orange')
duck = Duck(bill, tail)
duck.about()
This duck has a wide orange bill and a long tail.bill = Bill('wide orange')
duck = Duck(bill, tail)
duck.about()
bill0 = Bill('narrow rainbow')
tail0 = Tail('ginormous')
duck.about()
This duck has a wide orange bill and a long tail.tail0 = Tail('ginormous')
duck.about()
duck0 = Duck(bill0, tail0)
duck0.about()
This duck has a wide orange bill and a long tail.duck0.about()
From this I am forced to conclude that composition will only work with
particular instances of objects and not with any old objects created
from their respective classes. Is this understanding correct?
Thanks!
--
boB
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
boB
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor