Question: class WebBrowser: def _ _ init _ _ ( self , page ) : self.history = [ page ] self.current _ page = page self.is
class WebBrowser:
def initself page:
self.history page
self.currentpage page
self.isincognito False
def navigateself newpage:
self.currentpage newpage
if not self.isincognito:
self.history.appendnewpage
def clearhistoryself:
self.history:
@classmethod
def withincognitocls page:
instance clspage
instance.isincognito True
instance.history
return instance
The WebBrowser class
Print out the class method as shown in Snippet The output tells us that it is a bound method of the WebBrowser class. This illustrates what we said earlier regarding class methods and how they are bound to the class itself:
from main import WebBrowser
WebBrowser.withincognito
Snippet
Create a WebBrowser instance that starts off in incognito mode. Note that we call withincognito through the class. Despite not passing the cls argument in this call, Python implicitly passes the WebBrowser class to the function. All we need to pass in is the page parameter as shown in Snippet :
chrome WebBrowser.withincognitoshadywebsite.com"
chrome.isincognito
True
Snippet
Print out the current page of our instance to check whether it was set as shown in Snippet :
chrome.currentpage
'shadywebsite.com'
Snippet
Confirm that the history was not tracked as shown in Snippet :
chrome.history
Snippet
Additionally, you can call class methods through instances for the same effect as shown in Snippet :
opera WebBrowserfoobarcom"
netscape opera.withincognitosecretnet"
netscape.currentpage
'secret.net'
netscape.isincognito
True
Snippet
You should only call class methods through an instance in situations where it won't raise any confusion as to what kind of method it is you're calling instance or class method
Attach FileNo file chosen
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
