Do I always have to write "self." ?

Andrew Dalke dalke at acm.org
Wed May 3 22:18:24 CEST 2000


Samuel A. Falvo II wrote:
>I use context here in the same manner you used it in your reply to me
>("context of computer science"), as more or less a 'scope'.  Within the
>PrintName() method, 'name' can only be one type of object -- a string,
>because that's the only way it's used. 'name' is applicable *only* to
>PrintName() because that's the formal argument to PrintName().
>
>In more formal computer science terms, I, the reader of the source code,
can
>perform a type inference that 'name' is a string, because name is used
where
>a string is expected, and nowhere else.


As others have pointed out in this thread, you cannot make the inference
that "name" is a string.  It is something which can be stringified.
In Python, that's almost everything:

def PrintName( name ):
   print "The name is %s" % name

class Name:
  def __init__(self, first, last):
    self.first = first
    self.last = last
  def __str__(self):
    return "%s %s" % (self.first, self.last)


>>> PrintName("Andrew Dalke")
The name is Andrew Dalke
>>> PrintName(Name("Andrew", "Dalke"))
The name is Andrew Dalke
>>> PrintName(Name("THX", 1138))
The name is THX 1138
>>> PrintName(1138)
The name is 1138
>>> PrintName(3.1416)
The name is 3.1416
>>> PrintName(PrintName)
The name is <function PrintName at 1061020>
>>>

Granted, only the first two make sense, but it shows that the input does
not have to be of string type.

                    Andrew
                    dalke at acm.org






More information about the Python-list mailing list