Created
May 25, 2017 04:23
-
-
Save patwooky/8f10d01361f56dcdcec7687667a3e919 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class book(object): | |
classCount = 0 | |
def __init__(self, name='no name', numPages=0): | |
self.numPages = numPages | |
self.bookName = str(name) | |
book.classCount += 1 | |
def getNumPages(self): | |
return self.numPages | |
def setNumPages(self, numPages): | |
self.numPages = numPages | |
def getBookName(self): | |
return self.bookName | |
def setBookName(self, name): | |
self.bookName = name | |
def __repr__(self): | |
return self.getBookName() | |
def __add__(self, other): | |
return self.numPages + other | |
def __radd__(self, other): | |
return self.numPages + other | |
def __lt__(self, other): | |
return self.numPages < other | |
def __le__(self, other): | |
return self.numPages <= other | |
def __gt__(self, other): | |
return self.numPages > other | |
def __ge__(self, other): | |
return self.numPages >= other | |
def __eq__(self, other): | |
return self.numPages == other | |
def __ne__(self, other): | |
return self.numPages != other | |
def __del__(self): | |
book.classCount -= 1 | |
print('{} is deleted!'.format(self.bookName)) | |
return | |
booksList = [book(3)] | |
print(booksList[0].getNumPages()) | |
booksList[0].setNumPages(30) | |
booksList[0].getNumPages() | |
booksList.append(book(52)) | |
print('sum booksList = {}'.format(sum(booksList))) | |
print(booksList[0] + 2) | |
print('booksList[0] == booksList[1]: {}'.format(booksList[0]==booksList[1])) | |
print('booksList[1] > booksList[0]: {}'.format(booksList[1]>booksList[0])) | |
print('booksList[1] < booksList[0]: {}'.format(booksList[1]<booksList[0])) | |
print('books class count is {}'.format(book.classCount)) | |
# print(dir(book)) | |
newBook3 = book(99) | |
print('books class count is {}'.format(book.classCount)) | |
del newBook3 | |
print(book.classCount) | |
print('current book names: {}'.format([x for x in booksList])) | |
[booksList[x].setBookName('myBook_{}'.format(x)) for x in range(len(booksList))] | |
print('new book names: {}'.format([x for x in booksList])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment