Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Python Objects and Methods Question

Options
  • 20-04-2014 9:33pm
    #1
    Closed Accounts Posts: 2,338 ✭✭✭


    Hi,
    Assume you have code such as follows:
    import mechanize
     def viewSite(url):
       b = mechanize.Browser()
       page = b.open(url)
       source_code = page.read()
       print source_code
    viewSite(‘http://www.boards.ie/’)
    

    I don't understand why I have to create the page and source_code objects to get at the open()and read() methods.

    b.open(url) works on its own obviously.
    Why doesn't b.read work on it's own without using the page object

    More importantly, if I'm using a random module for the first time, how would I know to create the page and source_code objects to use the read method as above?
    I had a look at the Mechanize Pydoc and read is buried away here.
    I would never have found it if I hadn't got the above example code from a tutorial.

    I'm hoping some guidance will allow me to use more modules without reading tutorials on them first.


Comments

  • Registered Users Posts: 7,157 ✭✭✭srsly78


    Many filesystem apis do it this way, you call open, then read. Seems like the author of your library tried to mimic this convention. Call it "tradition".

    You will usually have to read tutorials/documentation to use new bits of software, get used to it.


  • Closed Accounts Posts: 2,338 ✭✭✭aphex™


    srsly78 wrote: »
    You will usually have to read tutorials/documentation to use new bits of software, get used to it.

    I'm happy to read module documentation, tutorials are harder to find are on random people's blogs/websites/books.

    I would never have found read() on the opening page of the module documentation here.

    Is there any other way to access read()?


  • Registered Users Posts: 7,157 ✭✭✭srsly78


    Try to find the real manual, not autogenerated documentation. Autogenerated stuff tends to be a bit verbose and confusing. If there are no tutorials or good documentation, then it's not a very good software library!


Advertisement