![]() |
confused about self, why not a reserved word?
class Foo(object):
def Hello(self): print "hi" object is purple, ie some sort of reserved word. why is self in black(ie a normal word) when it has special powers. replacing it with sel for example will cause an error when calling Hello. |
Re: confused about self, why not a reserved word?
2008/5/5, globalrev <skanemupp@yahoo.se>:
> class Foo(object): > def Hello(self): > print "hi" > > object is purple, ie some sort of reserved word. > > why is self in black(ie a normal word) when it has special powers. > replacing it with sel for example will cause an error when calling > Hello. Could you give an example of such an error? -- Regards, Wojtek Walczak http://www.stud.umk.pl/~wojtekwa/ |
Re: confused about self, why not a reserved word?
globalrev <skanemupp@yahoo.se> wrote:
> class Foo(object): > def Hello(self): > print "hi" > > object is purple, ie some sort of reserved word. > > why is self in black(ie a normal word) when it has special powers. Because `self` is just a name. Using `self` is a convention, but one can use any name which is not reserved. > replacing it with sel for example will cause an error when calling > Hello. Which error? I don't get one. 0:tolot:/tmp> python f.py hi 0:tolot:/tmp> cat f.py class Foo(object): def Hello(sel): print "hi" Foo().Hello() 0:tolot:/tmp> Marc |
Re: confused about self, why not a reserved word?
globalrev wrote:
> class Foo(object): > def Hello(self): > print "hi" > > object is purple, ie some sort of reserved word. It is not. > why is self in black(ie a normal word) when it has special powers. > replacing it with sel for example will cause an error when calling > Hello. That must be some other problem. This is perfectly legal python code and works: class Foo(object): def bar(imafancyobject): print imafancyobject f = Foo() f.bar() Diez |
Re: confused about self, why not a reserved word?
On Mon, 2008-05-05 at 04:57 -0700, globalrev wrote:
> class Foo(object): > def Hello(self): > print "hi" > > object is purple, ie some sort of reserved word. > > why is self in black(ie a normal word) when it has special powers. > replacing it with sel for example will cause an error when calling > Hello. > Wow. No it won't. sel is perfectly legal. I've seen s used on occasion, and of course metaclass methods often use cls. Direct ^C^V from my python interpreter: $ python Python 2.4.3 (#1, Dec 11 2006, 11:38:52) [GCC 4.1.1 20061130 (Red Hat 4.1.1-43)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> class Foo(object): .... def Hello(sel): .... print "hi" .... >>> f = Foo() >>> f.Hello() hi >>> Can you paste an example that breaks for you? -- Oook, J. Cliff Dyer Carolina Digital Library and Archives UNC Chapel Hill |
Re: confused about self, why not a reserved word?
sorry i noticed now a little error, i was playing around a lot and saw
that on the sel-example i had then called foo.hello() instead of foo.Hello(). but it was an informing error now i egt it. was wondering about self anyway so thanks for clearing it up. but it still is a bit confusing that u can pass with any word. wouldnt: class Foo(object): def Hello(): print "hi" make more sense? the class-thing seems inside out somehow in python. if im doing foo.Hello() im already saying that im using the class foo because i did foo=Foo() before and then still when calling Hello() i am invisibly passing the class itself a s a parameter by default? just seems backwards and weird. |
Re: confused about self, why not a reserved word?
> the class-thing seems inside out somehow in python. if im doing
> foo.Hello() im already saying that im using the class foo because i > did foo=Foo() before and then still when calling Hello() i am > invisibly passing the class itself a s a parameter by default? > just seems backwards and weird. You're not passing a reference to the class, you're passing a reference to the object. Any object-orientated programming language will be passing methods references to an object; Python just makes that explicit within the method definition. See http://www.python.org/doc/faq/general/#id35 |
Re: confused about self, why not a reserved word?
globalrev a écrit :
> sorry i noticed now a little error, i was playing around a lot and saw > that on the sel-example i had then called foo.hello() instead of > foo.Hello(). > > > but it was an informing error now i egt it. was wondering about self > anyway so thanks for clearing it up. > > > but it still is a bit confusing that u can pass with any word. > wouldnt: > > > class Foo(object): > def Hello(): > print "hi" > > > make more sense? What would make sens in your example would be to either make hello a staticmethod, since it doesn't use any reference to the current instance nor it's class. > the class-thing seems inside out somehow in python. Python's object model is indeed a bit pecular wrt/ most mainstream OOPLs. But once you understand how it works, it happens to be mostly well designed (IMHO) and really powerful. > if im doing > foo.Hello() im already saying that im using the class foo because i > did foo=Foo() Nope. You are saying your using an instance of class Foo. > before and then still when calling Hello() i am > invisibly passing the class itself a s a parameter by default? It's not the class that get passed (unless you use a classmethod, but that's not the case here), but the instance. And you are not "invisibly" passing it, you just pass it another way. In fact, foo.hello() is equivalent to type(foo).hello(foo). > just seems backwards and weird. Each and every OOPL needs to have a way to "inject" the instance a method was called on into the method's local namespace. Python solved this it's own way - by making 'methods' thin wrappers around instance/function couples, these wrappers being in charge of calling the function with the instance as first argument. From the implementation POV, this allow to build methods on existing, more general features - functions and the descriptor protocol - instead of having to special-case them. From the programmer's POV, this allow to define functions outside classes, then dynamically add them as 'methods' either on a per-class or per-instance basis. |
Re: confused about self, why not a reserved word?
"globalrev" <skanemupp@yahoo.se> wrote in message news:9c619edb-303b-439b-ad65-995fb76f70d5@y38g2000hsy.googlegroups.com... | sorry i noticed now a little error, i was playing around a lot and saw | that on the sel-example i had then called foo.hello() instead of | foo.Hello(). Lesson: whenever one inquires about an 'error', one should cut and paste the error traceback along with a relevant snippet of code. |
| All times are GMT. The time now is 09:29 AM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.