Reputation: 619
So I have two class that are called:
a.py
import requests
class A:
def __init__(self, parent):
self.parent = parent
self.accessToken = "VeryImportant" <---
def productFeed(self, Country, UserInput):
while True:
try:
else:
params = {
'country': UserInput,
'locale': 'en-{}_{}'.format(UserInput, UserInput)
}
headers = {
'importante': 'Yessir {}'.format(self.accessToken),
}
r = requests.get("URL", params=params, headers=headers, timeout=5)
# -------------------------------------------------------------------------
if r.status_code == 200:
return r.json()
b.py
import lib.vendors.testing.a as testing
class B:
def __init__(self):
self.getClassA = testing.A
def main(self):
country = "GB"
UserInput = "Hello"
print(self.getClassA.productFeed(country, UserInput))
However it seems like I am getting issue on class A: saying TypeError: ProductFeed() missing 1 required positional argument: 'UserInput'
The issue I believe is related to the self
- I assume I need to add it into the params inside the self.getClassA.productFeed(country, UserInput)
however the issue will be that if I send over the self, it doesn't want to use the self.accessToken from class A in that case and then I am not able to the the correct request.
What I want to do is that if I call self.getClassA.productFeed(country, UserInput)
- I want to use the self.accessToken that is in class A instead of needing to send it over
What should I do to be able to do that?
Upvotes: 0
Views: 45
Reputation: 608
You are not creating an instance of the A class. You use a direct reference:
testing.A
where you should use
testing.A( ... )
Your code is interpreting the country
variable as the self argument because self is not a keyword
Upvotes: 1
Reputation: 94
You can choose to use something as static. If "accessToken" isn't different for every instance of the class A you can define it before the init function. In that way you can read it by using A.accessToken and your program wont crash anymore. However if accessToken is static, this means that different instances of the class A cannot have different values for accessToken.
You can make the productFeed function static and change the self with anything else. When you call it you have to pass an instance of A as argument
In A class
@staticmethod
def productFeed(instance, Country, UserInput):
while True:
try:
else:
params = {
'country': UserInput,
'locale': 'en-{}_{}'.format(UserInput, UserInput)
}
headers = {
'importante': 'Yessir {}'.format(instance.accessToken),
}
r = requests.get("URL", params=params, headers=headers, timeout=5)
# -------------------------------------------------------------------------
if r.status_code == 200:
return r.json()
in main
def main(self):
country = "GB"
UserInput = "Hello"
print(self.getClassA.productFeed(A(None),country, UserInput))
Upvotes: 2