Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6871289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:47:13+00:00 2026-05-27T03:47:13+00:00

I have a polynomial class: class Polynomial: def __init__(self, *termpairs): self.termdict = dict(termpairs) To

  • 0

I have a polynomial class:

class Polynomial:
    def __init__(self, *termpairs):
        self.termdict = dict(termpairs)

To add an instance to this class, you enter the following:

P = Polynomial((3,2), (4,5), (9,8))

Now I’m trying to create a function that reads information from a text file, and from that text file, it creates an instance of the Polynomial class. The information is stored in the text file like this:

4 6
2 3
9 8
3 5
0 42

So far, the function looks like this:

def read_file(polyfilename):
    polyfilename = open("polyfilename.txt", "r")
    poly1 = polyfilename.read()
    polyfilename.close()
    poly1 = [line.split() for line in poly1.split("\n")]
    poly1 = [[int(y) for y in x] for x in poly1]
    for item in poly1:
        return Polynomial(item)

It only creates a Polynomial instance for the first pair in the text file, how can I make a Polynomial instance for all of the pairings in the text file?

EDIT:
I now have this as my function:

def read_file(polyfilename):
    polyfilename = open("polyfilename.txt", "r")
    poly = polyfilename.read()
    polyfilename.close()
    poly = [line.split() for line in poly.split("\n")]
    poly = [[int(y) for y in x] for x in poly]
    return Polynomial(poly[0], poly[1], poly[2], poly[3], poly[4])

It gives me the answer I’m looking for, however, the length of these text files can vary, so typing poly[1], poly[2] won’t work. Is there a way I can go through each index no matter what the length is?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T03:47:13+00:00Added an answer on May 27, 2026 at 3:47 am

    All you need is

    return Polynomial(*poly)
    

    instead of

    return Polynomial(poly[0], poly[1], poly[2], poly[3], poly[4])
    

    Docs here … read the paragraph starting with “””If the syntax *expression appears in the function call”””

    Bonus: Your function doesn’t use its arg, reuses the name poly like crazy, and isn’t idiomatic, so I rewrote the whole thing:

    def read_file(polyfilename):
        with open(polyfilename) as f:
            return Polynomial(*[[int(x) for x in line.split()] for line in f])
    

    HTH

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have a class called Polynomial, The initialization looks like this: def __init__(self,
I have code that looks like this: public class Polynomial { List<Term> term =
I have written a polynomial class along the lines described in SICP 2.5.3 (except
If I have a parent-child that defines some method .foo() like this: class Parent
So I have been developing a polynomial class where a user inputs: 1x^0 +
I have a nice polynomial, which is in fact the non-posted answer to this
I have a question regarding this keyword. I have a class called BiPoly, which
I have a class called Polynomial, and it stores information to the polynomial in
I have a feature set [x1,x2....xm] Now I want to create polynomial feature set
I have a polynomial class similar to one here: Polynomial.java . Except I haven't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.