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 8182231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T00:47:30+00:00 2026-06-07T00:47:30+00:00

I can’t understand how works the code below: class Host: name = None fileList

  • 0

I can’t understand how works the code below:

class Host:
    name = None
    fileList = []
    def __init__(self, hostName):
        self.name = hostName    
    def AddInfo(self,file):
        self.fileList.append(file)          
    def Cleanup(self):
        self.fileList   = []

I create 2 instances:

h1 = Host("hostName1")
h1.AddInfo("h1")
h1.Cleanup()
print h1.fileList, Host.fileList

h2 = Host("hostName2")
h2.AddInfo("h2")
print h2.fileList, Host.fileList

the result is:

h1.fileList = [], Host.fileList = ['h1']
h2.fileList = ['h1', 'h2'], Host.fileList = ['h1', 'h2']

why Host.fileList value is changed – I assigned new values to the instance only? why h2.fileList has such value – I was expecting ['h2'] here?

  • 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-07T00:47:35+00:00Added an answer on June 7, 2026 at 12:47 am

    This is probably easist to see by stepping through your code:

    h1 = Host("hostName1")  #After __init__:h1.filelist -> Host.filelist
    h1.AddInfo("h1")        #After AddInfo: h1.filelist -> Host.filelist
    h1.Cleanup()            #After Cleanup: h1.filelist is a new list
    
    h2 = Host("hostName2")  #After __init__:h2.filelist -> Host.filelist (which already has 'h1' inserted)
    h2.AddInfo("h2")        #After AddInfo: h2.filelist -> Host.filelist
    

    Inside your methods, when using self.filelist, python looks to see if the instance has a filelist attribute first. If the instance doesn’t have that attribute, python looks for the attribute on the class the instance belongs to. when you do self.filelist=[] in Cleanup, you are giving the instance the attribute filelist.

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

Sidebar

Related Questions

Can someone thoroughly explain the last line of the following code: def myMethod(self): #
Can you please help me understand the following code? It seems that here the
Can you guys help me understand a concept real quick, I'm having trouble understanding
can someone explain why the compiler accepts only this code template<typename L, size_t offset,
Can I register a .Net COM class with the SingleUse -flag? The reason I
Can't seem to figure out what's wrong with the simple getJSON call below. It's
Can we close all known/unknown connections to database with the code? I'm using Access
Does anyone know how can I replace this 2 symbol below from the string
Can i get the source code for a WAMP stack installer somewhere? Any help
Can we write all the control events in a seperate class file in different

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.