Next / Previous / Contents / Shipman's homepage

6.4. PathInfo.__init__(): The class constructor

This method is the one actually called when you use the class constructor PathInfo(). The single argument is the pathname of the desired file or directory.
# - - -   P a t h I n f o . _ _ i n i t _ _   - - -

    def __init__ ( self, path ):
        """Constructor for the PathInfo class.

          [ path is a string ->
              if path names an inode whose status is readable ->
                return a new PathInfo containing that status
              else -> raise OSError ]

The job of a constructor is to assemble a new instance of the class. In Python classes, all methods of a class have an invisible first argument self: this is the instance on which the methods operate. The instance can be thought of as a namespace—that is, a set of names and their corresponding values. When the constructor starts, self is a namespace containing all the names of methods in the class (and the class variables, of any), but no other names. The constructor then customizes the instance by adding or modifying the names in the namespace self.

The first order of business is to save the argument inside the instance's namespace, symbolized by self.
        #-- 1 --
        self.path  =  path

Next we ask the operating system for the status of that pathname.

There are two different functions in the os module for getting the status information about a path: os.stat() and os.lstat(). They work the same except when the path points to a soft link. In that case, os.stat() retrieves data about the path pointed to by the soft link, while os.lstat() retrieves data about the soft link itself. For our purposes, we want the latter. Both these methods raise an OSError exception if the given path is nonexistent or inaccessible.
        #-- 2 --
        # [ if path names an existing, accessible file path ->
        #     self.status  :=  the status tuple for path
        #   else -> raise OSError ]
        self.status  =  os.lstat ( path )

Then we pull out the items from the status tuple that we export: the file size, the creation and modification timestamps, and the mode bits. The constants that start with “ST_...” are the indices of elements of the status tuple, and come from the stat module.
        #-- 3 --
        # [ self.status is a status tuple ->
        #     self.size         :=  size from self.status
        #     self.createEpoch  :=  creation epoch time from
        #                           self.status
        #     self.modEpoch     :=  modification epoch time
        #                           from self.status
        #     self.mode         :=  mode bits from self.status ]
        self.size         =  self.status[stat.ST_SIZE]
        self.createEpoch  =  self.status[stat.ST_CTIME]
        self.modEpoch     =  self.status[stat.ST_MTIME]
        self.mode         =  self.status[stat.ST_MODE]

At this point we are done, because we have established the five stated invariants on the attributes .path, .size, .createEpoch, .modEpoch, and .mode.

Because this is a constructor (that is, because its name is .__init__()), we don't have to return a value explicitly. The constructor's first argument, self, is the instance we have constructed, and it is returned to the caller of the PathInfo() constructor.