Trying to understand a very simple class - from the book "dive intopython"

W

walterbyrd

Example 5.6. Coding the FileInfo Class
class FileInfo(UserDict):
"store file metadata"
def __init__(self, filename=None):
UserDict.__init__(self) (1)
self["name"] = filename (2)


What I do not understand is the last line. I thought 'self' was
supposed to refer to an instance of the object. But the square
brackets seem to indicate a list. Except you can not assign values to
a list like that. So where does "name" come from?

I am sure this is totally simple, but I missing something.
 
D

Dave Angel

walterbyrd said:
Example 5.6. Coding the FileInfo Class
class FileInfo(UserDict):
"store file metadata"
def __init__(self, filename=ne):
UserDict.__init__(self) (1)
self["name"] =ilename (2)


What I do not understand is the last line. I thought 'self' was
supposed to refer to an instance of the object. But the square
brackets seem to indicate a list. Except you can not assign values to
a list like that. So where does "name" come from?

I am sure this is totally simple, but I missing something.
You're deriving your class from UserDict, which is a "Class that
simulates a dictionary." So you can use dictionary semantics on
"self." "name" is a key for the dict.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top