0

I am trying to assign the return values of getItem and getBuyingType to the self variables in the __init__ method in my Ebay Scraper function. How can I do this? If it's not possible, is there another way to assign the outputs of these two functions to be part of the Ebay Scraper class? Item should be assigned to self.item and buying_type to self.buying_type.

class EbayScraper(object): def __init__(self): self.base_url = "https://www.ebay.com/sch/i.html?_nkw=" self.item = self.buying_type = self.url_seperator = "&_sop=12&rt=nc&LH_" self.url_seperator2 = "&_pgn=" self.page_num = "1" self.currentPage = 1 def getItem(self): item = input("Item: ") return item def getBuyingType(self): buying_type = input("Please specify a buying type (Auction or Buy It Now): ") buying_type = buying_type.lower() if buying_type == "auction": return buying_type + "=1" elif buying_type == "buy it now": return buying_type + "=1" else: print("Invalid buying type specified.") self.getBuyingType() 
3
  • 1
    self.item = self.getItem() ? Commented Jan 16, 2020 at 19:15
  • 2
    Don't call getBuyingType recursively when you should just be using a loop instead. Commented Jan 16, 2020 at 19:16
  • 1
    It's not the job of EbayScraper to prompt the user for input; that's the responsibility of whoever is using EbayScraper. At most, you should define a method that simply takes an argument and assigns that to self.item, or just assign to the attribute directly; getters aren't always necessary in Python. Commented Jan 16, 2020 at 19:17

2 Answers 2

2

You can call functions inside __init__ method

def __init__(self): ... self.item = self.getItem() 
Sign up to request clarification or add additional context in comments.

Comments

1

The proper way to do this is to simply pass arguments to __init__ to initialize the values. If you want to provide an interactive method for providing those arguments, define a class method.

class EbayScraper(object): def __init__(self, item, buying_type): self.base_url = "https://www.ebay.com/sch/i.html?_nkw=" self.item = item self.buying_type = buying_type self.url_seperator = "&_sop=12&rt=nc&LH_" self.url_seperator2 = "&_pgn=" self.page_num = "1" self.currentPage = 1 @classmethod def prompt_user(cls): item = input("Item") while True: buying_type = input("Please specify a buying type...").lower() if buying_type in ('auction', 'buy it now'): break print("Invalid buying type specified") return cls(item, buying_type + "=1") e = EbayScraper.prompt_user() 

3 Comments

Why define it as a @classmethod ? Couldn't the same be accomplished on an instance level without fiddling with the class?
The main purpose of a class method is to provide an alternate means of constructing an object. The canonical way would be to simply pass arguments to __init__, like EbayScraper("book", "auction"). This class method simply wraps the code that prompts the user for those values before creating the instance and returning it.
Inside prompt_user, cls is just a reference to EbayScraper (or possibly a subclass of EbayScraper).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.