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?

3 Answers 3

3

This is basic Python (as in "Python is not Java").

Attributes declared at the class level are class attributes, not instance attributes. To declare an instance attribute, assign directly to self inside a method, as you do with self.name in __init__.

Sign up to request clarification or add additional context in comments.

2 Comments

I know this example is ambiguous - I will never use such code in real. I'm just trying to understand why class level variable Host.fileList has such value in my example?
It's not ambiguous at all, and your code is fine if you want a class variable. It has that value because that's how the semantics of Python are defined: accessing a class variable via self is allowed, but it's still a class variable.
2

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.

2 Comments

Do you mean the instance variable was created just after "=" in Cleanup not in self.fileList.append?
@Francheska -- Of course self.fileList.append doesn't create a new instance variable. I'm not sure I understand your comment. I've added After to the comments in the code above. Does that clear it up?
2

Variables in class scope are defined by class. You should create your variables in __init__.

def __init(self, hostName):
    self.name = hostName
    self.fileList = []

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.