Question

From this page: http://www.brpreiss.com/books/opus7/html/page94.html

enter image description here

  1. Why is fget=lambda self: self.getNumberOfColumns() used? I understand that this is to be able to use Matrix().numberOfColumns (as an attribute), but what is really happening here?

  2. Why can't we do fget=self.getNumberOfColumns() instead of using the lambda function?

I am having a hard time understanding this. Can someone please explain?

Was it helpful?

Solution

You can't do fget=self.getNumberOfColumns because self isn't declared in the class scope.

However, the lambda isn't necessary; you can write it as

numberOfRows = property(fget=getNumberOfRows)

or, even better:

@property
def numberOfRows(self):
    return self._numberOfRows

without the need for an explicit getter, property call and assignment.

OTHER TIPS

You could avoid the lambda like this:

@property
def numberOfColumns(self):
    return self.getNumberOfColumns()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top