[Tutor] Aggregation vs Composition
Good Day All, I am new to Python and I came across the concept of Composition and Aggregation the other day in Dusty Philips's Python 3: Object-Oriented Programming book. Based on my reading, what I gathered was that Composition implies a relationship where the child cannot exist independent of the parent while Aggregation, on the other hand, implies a relationship where the child can exist independently of the parent. However, in one of the paragraph of the book, *Dusty mentioned that composition is aggregation* (refer to the snapshot below, the sentence which has been highlighted in yellow). I am really confused by this statement and I appreciate that if someone could enlighten me on this as I do not really see how aggregation can be equivalent to composition if the child in one case can exist independently while the other could not exist independently of the parent. [image: Inline image 1] Thanks and Regards, JY ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Question
I am working on a program where I found the line: x,y,z = np.loadtext('abcabc.txt', unpack= True, skiprows =1) What does the x, y, z thing mean? What does the unpack= True mean? Thank you -- Khabbab Zakaria Dept of Power Engineering Jadavpur University Calcutta India ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Aggregation vs Composition
On 10/12/17 05:07, jia yue Kee wrote: > in Dusty Philips's Python 3: Object-Oriented > Programming book. Caveat: I've not read this book so can only guess at what the author might be meaning. > Based on my reading, what I gathered was that Composition implies a > relationship where the child cannot exist independent of the parent while > Aggregation, on the other hand, implies a relationship where the child can > exist independently of the parent. Correct. But the key word here is "implies". In some languages the differences can be directly implemented in the language but in Python the relationships are always just implied. The differences do not really exist. (Unless you go to inordinate lengths to hide and link the data, which is rarely, if ever, justified.) > However, in one of the paragraph of the book, *Dusty mentioned that > composition is aggregation* I'm guessing he means that in Python we implement both concepts in the same way whether in a collection or a class (which in Python can be thought of as a special type of collection). This is because all values in Python are objects and variables(names) are simply references to those objects. So every object in Python must exist independent of its parent to some degree. > (refer to the snapshot below, the sentence > which has been highlighted in yellow). This is a text mailing list so graphics get stripped by the server, sorry. SEnd a URL if necessary. > I am really confused by this I think he is talking about the practical limitations in Python such that pure Composition does not really exist but is implemented *as a concept* by aggregation. But as I said I haven't read the book so am guessing. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Question
On 10/12/17 05:48, Khabbab Zakaria wrote: > I am working on a program where I found the line: > x,y,z = np.loadtext('abcabc.txt', unpack= True, skiprows =1) > What does the x, y, z thing mean? > What does the unpack= True mean? They are related. unpacking is a feature of Python whereby a collection of values can be assigned to individual variables in one statement. Consider a list of numbers: nums = [1,2,3] we can unpack those 3 values into separate variables like so x,y,z = nums This is equivalent to: x = nums[0] y = nums[1] z = nums[2] So in your example the numpy function returns some kind of collection of 3 values which are unpacked into x,y and z exactly as we did with nums above. I assume the unpack=True argument simply controls the output of the function such that unpacking is possible, but I'm not familiar with it so cannot be sure. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Question
Hello Khabbab Zakaria, On Sun, Dec 10, 2017 at 11:18:16AM +0530, Khabbab Zakaria wrote: > I am working on a program where I found the line: > x,y,z = np.loadtext('abcabc.txt', unpack= True, skiprows =1) > What does the x, y, z thing mean? "x, y, z = ..." is iterable unpacking. The right hand side has to be an iterable (any object that can be iterated over in a for-loop) such as a list, a tuple, a set, or similar. For example: x, y, z = [1, 2, 3] will set x = 1, y = 2, z = 3. It is an error if the object on the right hand side has too few or too many items: a, b, c, d, e = (1, 2, 3) # Too few items a, b, c, d, e = (1, 2, 3, 4, 5, 6, 7) # Too many items > What does the unpack= True mean? I'm afraid you will need to read the numpy documentation for that. I tried looking at it myself, but the version of numpy I have seems to be too old: py> import numpy as np py> help(np.loadtext) Traceback (most recent call last): File "", line 1, in AttributeError: 'module' object has no attribute 'loadtext' -- Steve ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Aggregation vs Composition
I own this book, too. I'll insert the portions of the text that I believe the OP is referring to. On Sun, Dec 10, 2017 at 3:01 AM, Alan Gauld via Tutor wrote: > On 10/12/17 05:07, jia yue Kee wrote: > >> in Dusty Philips's Python 3: Object-Oriented >> Programming book. > > Caveat: I've not read this book so can only > guess at what the author might be meaning. > >> Based on my reading, what I gathered was that Composition implies a >> relationship where the child cannot exist independent of the parent while >> Aggregation, on the other hand, implies a relationship where the child can >> exist independently of the parent. The author defines composition on page 17 as: "Composition is the act of collecting together several objects to compose a new one. Composition is usually a good choice when one object is part of another object." > Correct. But the key word here is "implies". > In some languages the differences can be directly > implemented in the language but in Python the > relationships are always just implied. The differences > do not really exist. (Unless you go to inordinate > lengths to hide and link the data, which is rarely, > if ever, justified.) > >> However, in one of the paragraph of the book, *Dusty mentioned that >> composition is aggregation* On page 18 the author goes on to use a chess set as an example aiming to use object-oriented design for a computer chess game. I _think_ the OP may be referring to this paragraph: "The chess set, then, is composed of a board and thirty-two pieces. The board is further comprised of sixty-four positions. You could argue that pieces are not part of the chess set because you could replace the pieces in a chess set with a different set of pieces. While this is unlikely or impossible in a computerized version of chess, it introduces us to *aggregation*. Aggregation is almost exactly like composition. The difference is that aggregate objects can exist independently. It would be impossible for a position to be associated with a different chess board, so we say the board is composed of positions. But the pieces, which might exist independently of the chess set, are said to be in an aggregate relationship with that set." He continues in the next paragraph: "Another way to differentiate between aggregation and composition is to think about the lifespan of the object. If the composite (outside) object controls when the related (inside) objects are created and destroyed, composition is most suitable. If the related object is created independently of the composite object, or can outlast that object, an aggregate relationship makes more sense. Also keep in mind that composition is aggregation; aggregation is simply a more general form of composition. Any composite relationship is also an aggregate relationship, but not vice versa." I think it is these last two sentences that are confusing the OP. Hope this provides more context to help the OP. boB ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor