0

I'm using memory.py from fusepy (http://code.google.com/p/fusepy/source/browse/trunk/memory.py) for a programming assignment.

How does setxattr (line 87) actually accomplish anything? self.files isn't modified in any way and attrs is destroyed when the function exits.

1 Answers1

1
def setxattr(self, path, name, value, options, position=0):
    # Ignore options
    attrs = self.files[path].setdefault('attrs', {})
    attrs[name] = value

The side-effect is achieved by setdefault, which creates a new item in self.files[path] (unless attrs already exists as a key in it), and returns a reference to the value.

Next, that reference is modified, by assigning to its key name, the value value. By that operation too, self.files is modified.

shx2
  • 61,779
  • 13
  • 130
  • 153
  • Where can I see what setdefault does? I knew it could be done is attrs pointed to a piece of self.files, but I think I read python doesn't really do pointers. My background is much stronger in C++, python is totally new to me. – user2921279 Oct 25 '13 at 19:45
  • if you're used to C++, think about it this way: *everything is a pointer* in python – shx2 Oct 25 '13 at 19:48