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

  • Home
  • SEARCH
  • 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 8097765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T21:51:56+00:00 2026-06-05T21:51:56+00:00

I’m apparently having some trouble learning how to use classes correctly. I expect the

  • 0

I’m apparently having some trouble learning how to use classes correctly. I expect the following code to loop through a set of files (it does), adding elements to a set as it goes (it does), and starting the set over when it moves to a new file (it does not). The way I’ve hacked at the code below, I would expect it to return an empty set each time for file.terminals, because I believe that each set is the unique attribute of a new instance of the class SrcFile. Apparently, I am mistaken. What actually happens is that the set of terminals created by the first file persists (and is added to) for all subsequent files in the list, although other attributes (e.g. file.name, file.seqlength()) change appropriately with the files. Can someone please explain why it works this way and how I can create the behavior that I’m expecting? Thanks in advance for your patience with a beginning programmer’s ignorance.

class SrcFile:
   terminals = set([])
   def __init__(self, which):
      self.name = which
   def seqlength(self):
      with open(self.name) as file:
         linecounter = 0
         for line in file:
            linecounter += 1
            if linecounter == 3:
               return int(line.split()[0])
               break

class Record(SrcFile):
   def terminal(self):
      record = self.name
   def terminal(self):
      record = self.name
      return record.split()[0]

for f in files:
   file = SrcFile(f)
   print(file.name, file.seqlength(), file.terminals)

   with open(f) as f:
      recordline = re.compile(r"^([A-Z]{5})\s{3}")
      for line in f:
         if recordline.match(line):
            record = Record(line)
            if record.terminal() in file.terminals:
               pass
            else:
               file.terminals.add(record.terminal())
  • 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-06-05T21:51:58+00:00Added an answer on June 5, 2026 at 9:51 pm

    You are defining terminals within the class declaration, not within the __init__ function that’s executed for every new instance of the class.

    Basically, terminals is initialized for the whole class, not for each instance of the class:

    >>> SrcFile.terminals
    set([])
    

    Notice I ran SrcFile.terminals, not SrcFile().terminals, which means that SrcFile is a reference to the class, not an instance of the class.

    You put variables like that inside of the __init__ function to make them instance-specific:

    class SrcFile(object):
      def __init__(self, which):
        self.name = which
        self.terminals = set([])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am trying to loop through a bunch of documents I have to put
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I want use html5's new tag to play a wav file (currently only supported

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.