inheriting file object

harold fellermann harold.fellermann at upf.edu
Wed Jul 6 15:00:30 EDT 2005


> I don't know if I should be inheriting file or just using a file 
> object.
>   How would I determine which one would be more appropriate?

Inheritance is often refered to as an IS relation, whereas using an 
attribute
is a HAS relation.

If you inherit from file, all operations for files should be valif for 
your
class also. Usually the file-operations would be directly inherited and 
not
overwritten.

However, if you don't want to expose all file functionalities, a HAS 
relation
is more appropriate. if you plan to use your class as a file handle, 
e.g. for
formatting output in a special way, I woould prefer to make the file an 
attribute:

class myFile :
	def __init__(self,fname,mode="r") :
		self.file = file(fname,mode)

	def write_formatted(self,string) :
		# format string
		self.file.write()


If you would tell as your use case, it would be easier to give you an 
advice.

- harold -

--
Yesterday is but today's memory and Tomorrow is today's dream.
--




More information about the Python-list mailing list