Let's say I have a file "icon.ico" and an url "url.com".
The'll be used just once within the class - "icon.ico" will be set to some window and we'll do the request to url in one method.
I have three ways to define these variables.
1-st way - define as global constants
#in the top of the file
ICON = "icon.ico"
URL = "http://url.com"
#and then
def setIcon(self):
self.setWindowIcon(QtGui.QIcon(ICON))
def getData(self):
content = requests.get(URL).content
2-nd way - define as variables of the class
def __init__(self):
self.url = "http://url.com"
self.icon = "icon.ico"
3-rd way - define just within the method the'll be used
def setIcon(self):
icon = "icon.ico"
def getData(self):
url = "http://url.com"
Instead of:
def __init__(self):
self.url = "http://url.com"
self.icon = "icon.ico"
or
def setIcon(self):
icon = "icon.ico"
is preferible:
def __init__(self, url, icon):
self.url = url
self.icon = icon
or, if you think the values are going to be 90% the same:
def __init__(self, url="http://url.com", icon="icon.ico"):
self.url = url
self.icon = icon
1-st way - define as global constants
2-nd way - define as variables of the class
3-rd way - define just within the method the'll be used
4-th way - as a class level constant
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With