[Tutor] Methods and classes

Chris Hengge pyro9219 at gmail.com
Wed Sep 13 23:57:12 CEST 2006


OK, now that you asked for an example of a class using 'self', I can't
find it..so either the thing I was reading was wrong... or I dreamed it
up.. or it was something specific that I was looking at that decided I
wasn't interested in and now I don't remember. 

As for a quick example of the methods using (self, variables)..
the first example I've found of my list of resources is the deitel book
"Python - How to Program" Every method I've seen in this particular book
places 'self' as a parameter for each method. I am sure I've seen this
in at least one other book, as well as a few websites I've hit.

The deitel book has a note on page 229:
	Failure to specify an object reference (usually called self) as the
first parameter in a method definition causes fatal logic errors when
the method is invoked at runt-ime.

Now I've got methods all over the place among several scripts that don't
use self, and each other works fine as I'd expect. 

Thanks for the other information!

On Thu, 2006-09-14 at 09:18 +1200, John Fouhy wrote:
> On 14/09/06, Chris Hengge <pyro9219 at gmail.com> wrote:
> > Can anyone explain what I've been reading? I'm trying to understand why
> > many documents show:
> >        def myMethod(vars):
> > or
> >        class myClass(var):
> > and others show:
> >        def myMetheod(self, vars)
> > or
> >        class myClass(self, vars)
> 
> Um.  Can you give an example of something saying "class myClass(self,
> vars)" ?  The "arguments" to a class are other classes that you want
> to inherit from, and are different from function arguments!
> 
> As to your other question, though ---
> 
> Suppose I have a class:
> 
> class MyClass(object):
>     # etc
> 
> And suppose I create an instance of that class:
> 
> mc = MyClass()
> 
> And then I call a method on that instance:
> 
> mc.someFunc(3, 'foo')
> 
> Although I have given the function someFunc two arguments, it will
> actually be passed _three_ arguments.  The first argument will be mc
> itself.  So, in the definition of MyClass, you would have:
> 
>     def someFunc(self, x, s):
>         # etc
> 
> "self" is the name traditionally given to the first parameter, which
> receives the class instance.  If you wanted to make things explicit,
> you could instead do:
> 
> MyClass.someFunc(mc, 3, 'foo')
> 
> I think this is exactly equivalent to mc.someFunc(3, 'foo').  (someone confirm?)
> 
> On the other hand, if you're writing a utility function that's not
> part of a class, you won't give it a "self" parameter.
> 
> Hope this helps :-)
> 
> > Also, what is with the double underscores? (__init__ for example) is
> > this required? or a Pythonic naming convention? When and how to use?
> 
> Various special methods have the form "__xxx__".  For example, the
> builtin function str converts things into strings.  str is associated
> with the special method __str__.  If you write:
> 
> s = str(o)
> 
> this is equivalent to:
> 
> s = o.__str__()
> 
> and you can define / override the __str__ function in your own classes
> to control how they are converted to strings.
> 
> Also, there is a convention that variable names starting with a single
> underscore are private (since there's no "true" concept of
> private/public in python).  Variable names starting with two
> underscores are "more private", and python mangles the name a bit.
> 
> eg, try the following:
> 
> class Foo(object):
>     def __init__(self):
>         self.x = 1
>         self._y = 2
>         self.__z = 3
> 
> f = Foo()
> print f.x
> print f._y
> print f.__z
> 
> > I'm 'trying' to write clear pythonic code since in all reality it gives
> > a nice polish to the code when compared to writing c style.
> 
> I don't think you'll find many here who disagree with that :-)
> 



More information about the Tutor mailing list