On 14/09/06, Chris Hengge <[EMAIL PROTECTED]> 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 :-) -- John. _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor