I'm just getting to grips with python, and am currently trying to change the value of a Parent class variable using a subclass method. A basic example of my code is below.
from bs4 import BeautifulSoup
import urllib.request as req
url = "http://www.google.com"
r = requests.get(url)
soup = BeautifulSoup(r.content, "lxml")
new_url = input("Please enter a URL: ")
request = req.Request(new_url)
response = req.urlopen(request)
Parent.url = new_url
run = Child()
>>> Please enter a url: www.thisismynewurl.com
>>> #Prints output for www.google.com
class Parent(object): url = "http://www.google.com" r = requests.get(url) soup = BeautifulSoup(r.content, "lxml") print(url)
The code to assign the
soup is run once, at time of class definition.
Therefore no changes to
Parent.url will be reflected in calls to the
random_method, because the soup is already collected.