Arg names in class and function code - Newbie question

Roy Smith roy at
Tue Apr 29 00:46:01 CEST 2003

newspost at (Lol) wrote:

> HI all,
> 	Could anyone let me know - do the names you give as arguments HAVE to be
> actual variable names?
> e.g def readFile(filename,dir):
> 	blah blah
> 	return blah
> or even class FileRead(filename,dir):
> 	def doSomething (self,this):
> 		yadda yadda
> 		return yadda
> In the above examples do filename and dir have to be the name of the
> variables to pass as args to the class/functions OR are they descriptive
> so I can pass the real name of the file and directory?

I'm not completely sure I understand what you're asking, but if you're 
asking what I think you're asking, you're confused.  If not, them I'm 
confused.  Pressing on with the first assumption in mind....

When you say

def readFile (filename, dir):

the variables "filename" and "dir" are what's known in computer science 
circles as "formal parameters".  Basicly, you're saying that readFile is 
supposed to get called with two arguments.  This creates two variables 
for this function to use, "filename", which will get initialized to the 
value of the first argument, and "dir" which will get initialized with 
the value of the second.

Their names can be anything you like.  They need not (and usually don't) 
match the variable names used in the part of the program which calls the 
function.  You can call it with constants if you like:

def readFile (filename, dir):
   blah blah
   return blah

readFile ("eggs", "/spam/spam/spam/spam/spam/spam/spam")

As for the

class FileRead(filename,dir):
    def doSomething (self,this):
        yadda yadda
        return yadda

I'm really not sure what you're trying to do there.  As written, you've 
defined a class which inherits from a class who's name is in filename, 
and from another class who's name is in dir (at the time the class 
statement is executed).  This is legal, but I suspect it's not what you 
had in mind :-)

Perhaps you were thinking of an __init__() method which would take those 
two arguments?  In which case, you'd have something like:

class FileRead:
    def __init__ (self, filename, dir):
        self.file = open (dir + '/' + filename)

    def doSomething (self, this):
        yadda yadda
        return yadda

As a matter of style (and perhaps I'm really picking a nit here), I'd 
avoid "this" as a variable name.  In Python, "self" is the standard name 
for an object's handle to itself, and "this" plays that role in C++.  
There's nothing inherently wrong with using "this" as a Python varible 
name, but it just seems like a potential source of confusion.

More information about the Python-list mailing list