-
-
Save gbertoncelli/fa3a99fa4a47d1a5f0b7fba8d857aae4 to your computer and use it in GitHub Desktop.
Objs, special methods, imports, files
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
# import di classi esterne al mio file principale | |
from module import Book | |
from module import Dictionary | |
book = Book("Moby Dick", "Loomings. \ | |
Call me Ishmael. Some years ago—never mind how\ | |
long precisely...", 500) | |
book.read() | |
print("Implementation of special method __len__:") | |
for x in range(len(book)): | |
print(x + 1, end=" ") | |
print("pages!") | |
myFirstDictionary = Dictionary("Garzanti", "Apple = mela, mela = apple", 1, "English -> Italian and viceversa") | |
print("The Dictionary is a Book: ", issubclass(Dictionary, Book)) | |
print("myFirstDictionary is a Dictionary: ", isinstance(myFirstDictionary, Dictionary)) | |
print("myFirstDictionary is a Book: ", isinstance(myFirstDictionary, Book)) | |
print("I'm printing on a file...") | |
#with construct | |
with open("myFile.txt", "a") as file: | |
print("I've printed on a filme many times!:D", file=file) # a = append: aggiunta non in sovrascrittura! | |
print("Printing my book:\n", book) | |
print("Calling private method from other function:") | |
book.callMyPrivateMethod() | |
print("Calling it directly:") | |
book.__privateMethod() |
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: | |
def __init__(self, title, body, pages): | |
self.title = title | |
self.body = body | |
self.pages = pages | |
def read(self): | |
print(self.body) | |
def __privateMethod(self): | |
print("You can't call me from outside!") | |
def callMyPrivateMethod(self): | |
self.__privateMethod() | |
def __len__(self): | |
return self.pages | |
def __str__(self): | |
return "Title: {0}\nBody: {1}\nFor {2} pages...:O".format(self.title, self.body, self.pages) | |
class Dictionary(Book): | |
def __init__(self, title, body, pages, languages): | |
# calling super-constructor | |
super(Dictionary, self).__init__(title, body, pages) | |
self.languages = languages | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment