Re: [Tutor] question about classes and atributes
Because your atribute is a class attribute: class C: ca = 123 print C.ca # 123 c1 = C() print c1.ca# 123 c1.ca = 140 print c1.ca# 140 print C.ca # 123 c2 = C() print c2.ca# 123 C.ca = 141 print C.ca # 141 print c1.ca# 140 print c2.ca# 141 Basically, when an instance does not have an attribute, it looks them up in the class, which might recurse into base classes. Furthermore, objects classes 101 material: class C: def method(self): print self c = C() print c.method # bound method to c print C.method # unbound method, checks that first argument is a C print C.__dict__[method] # function, does NOT check first argument. So basically, the whole self argument handling is magic that happpens during attribute lookup. Andreas Am Freitag, den 03.11.2006, 14:27 +0100 schrieb euoar: I think I don't understand the OOP in python, could anyone explain why this code works? class example: atribute = hello world print example.atribute Why you don't have to make an object of the class to access to the atribute? ( class example: atribute = hello world obj = example() print obj.atribute Thanks in advance. __ LLama Gratis a cualquier PC del Mundo. Llamadas a fijos y móviles desde 1 céntimo por minuto. http://es.voice.yahoo.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor signature.asc Description: Dies ist ein digital signierter Nachrichtenteil ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] question about classes and atributes
Andreas Kostyrka escribió: Because your atribute is a class attribute: class C: ca = 123 print C.ca # 123 c1 = C() print c1.ca# 123 c1.ca = 140 print c1.ca# 140 print C.ca # 123 c2 = C() print c2.ca# 123 C.ca = 141 print C.ca # 141 print c1.ca# 140 print c2.ca# 141 Basically, when an instance does not have an attribute, it looks them up in the class, which might recurse into base classes. Furthermore, objects classes 101 material: class C: def method(self): print self c = C() print c.method # bound method to c print C.method # unbound method, checks that first argument is a C print C.__dict__[method] # function, does NOT check first argument. So basically, the whole self argument handling is magic that happpens during attribute lookup. Andreas Am Freitag, den 03.11.2006, 14:27 +0100 schrieb euoar: I think I don't understand the OOP in python, could anyone explain why this code works? class example: atribute = hello world print example.atribute Why you don't have to make an object of the class to access to the atribute? ( class example: atribute = hello world obj = example() print obj.atribute Thanks in advance. __ LLama Gratis a cualquier PC del Mundo. Llamadas a fijos y móviles desde 1 céntimo por minuto. http://es.voice.yahoo.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor Thank you for your answer and the examples. So without self it is an instance variable (like static in java/c#). But then, I don't understand how is it possible this in your example: c1.ca = 140 or down: print C.method Are you creating new atributes and methods at run time? Is that what has happened? In fact I have tried also this: class example: atribute = green obj = example() obj.a_new_atribute = white And even this seems to be correct: class example: atribute = green example._other_atribute = yellow So, in python, you can add methods at run time to an object, and even you can add them to a class at run time? __ LLama Gratis a cualquier PC del Mundo. Llamadas a fijos y móviles desde 1 céntimo por minuto. http://es.voice.yahoo.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] question about classes and atributes
I think I don't understand the OOP in python, could anyone explain why this code works? class example: atribute = hello world print example.atribute Why you don't have to make an object of the class to access to the atribute? because that attribute is part of the Class object that's created when you declare a class. As you can see by the following code: class example: attribute = 'hello, world!' example class __main__.example at 0x00B528D0 there is actually a type of object called 'class.' when you make an instance of a class, a = example() a __main__.example instance at 0x00B40440 it is now a example object instance. Thank you for your answer and the examples. So without self it is an instance variable (like static in java/c#). But then, I don't understand how is it possible this in your example: c1.ca = 140 Because c1 is an instance of the class 'C', it has the attribute .ca already in it. This is a reference to the class attribute 'ca' which is equal to 123. However, when you change c1.ca, because the class attribute 'ca' is immutable (since it's an integer) a local copy of ca is created with the value 140 in it. C.ca is still 123. If you now do C.ca = 234, c1.ca is still 140, because it now has a local (instance) attribute called 'ca' that hides the class attribute. However, if you do something like this... class C: ca = 123 c1 = C() C.ca = 567 print c1.ca you will get an output of '567'. because c1 never got the local instance attribute to replace the reference to the class-wide attribute, so changing the C.ca will also affect c1.ca. or down: print C.method Are you creating new atributes and methods at run time? Is that what has happened? In fact I have tried also this: class example: atribute = green obj = example() obj.a_new_atribute = white And even this seems to be correct: class example: atribute = green example._other_atribute = yellow So, in python, you can add methods at run time to an object, and even you can add them to a class at run time? No, you're confusing Python with a compiled language. You're not adding methods to an object at run-time because there's not a distinction between runtime and compile-time in Python, because compile-time doesn't exist. You can add attributes to classes any time you want inside your program. Just like I can add an element to a list any time I want. a = [1,2,3] a.append(4) Classes are just objects, as lists are, and integers are, and everything else is as well. And when I execute the code, Python knows how to do all of these things. You see, an interpreted session is not the same as you think of 'at run time' being. For the most part, an interpreted session is exactly the same as if I were to type the code into a text document, save it, and execute it. So yeah, anywhere in your program you can add methods to classes, but really saying 'at run-time' is confusing terminology. It implies that if I were running someone else's program I could just add methods in on the fly whenever I wanted to. This is not true, unless they've enabled this functionality. HTH, -Luke ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] question about classes and atributes
euoar [EMAIL PROTECTED] wrote in Thank you for your answer and the examples. So without self it is an instance variable (like static in java/c#). Without self it is a class attribute like static etc in C++/Java. An instance variable is one that is unique to an instance! Although I think it may be more powerful since I seem to recall that static members are not accessible via inheritance whereas Python class variables are. Also i'm not sure if statics can be reached via an instance whereas Python class variables can. But my Java/C# is very rusty on statics... Note also that you can go even further by specifying class methods too but they need special syntax. If you are only familiar with Java style statics you might find the concept of class variables and methods a little different in Python, which follows the traditional OOP style of Lisp and SmallTalk rather than the hybrid OOP style of Java etc. That is, a class variable/method is usually treated as one that applies to the class itself, or one that is shared by all instances. Java tend to use static methods as a replacement for traditional functions, ie. things you can do without creating an instance. You can do both things in any of the languages but conceptually they tend to be treated differently, especially since Python supports stand-alone functions. Are you creating new atributes and methods at run time? Is that what has happened? In fact I have tried also this: Yes, Python classes are a special type of container (really a special type of dictionary) , so just as you can add new keys to a dictionary you an add new attributes to a class or object at run time. So, in python, you can add methods at run time to an object, and even you can add them to a class at run time? I'm not sure about adding methods at run time, I've never tried it but I think the magic around the self parameter might not work. But you can definitely add attributes. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] question about classes and atributes
Alan Gauld wrote: euoar [EMAIL PROTECTED] wrote in So, in python, you can add methods at run time to an object, and even you can add them to a class at run time? I'm not sure about adding methods at run time, I've never tried it but I think the magic around the self parameter might not work. But you can definitely add attributes. Sure it works: In [1]: class foo(object): pass ...: In [2]: f=foo() In [3]: f.show() --- type 'exceptions.AttributeError'Traceback (most recent call last) D:\Projects\e3po\ipython console in module() type 'exceptions.AttributeError': 'foo' object has no attribute 'show' In [4]: def show(self): print Hi, I'm a foo ...: In [5]: foo.show=show In [6]: f.show() Hi, I'm a foo More advanced explanation: The magic around the self parameter is actually built-in to every function object (or its class, anyway). Functions have __get__() methods which means they can be used as descriptors. When Python evaluates f.show(), quite a few steps happen: - find the value of the show attribute in the class definition. This finds the show function object. - the function object has a __get__() method, so call show.__get__(obj) where obj is the original object being accessed. - the __get__() method wraps obj and the original function into a new callable object and returns that. - finally the temporary callable is actually called (by calling its __call__() method) and the wrapper adds the self parameter to the argument list and dispatches to the wrapped (original) function. The descriptor mechanism is only used for class attributes, not instance attributes, so if you want to add a method to an individual instance you have to do a little more work using new.instancemethod: In [12]: def show2(self): print I'm still a foo : The naive approach won't work: In [14]: f.show2 = show2 In [15]: f.show2() type 'exceptions.TypeError': show2() takes exactly 1 argument (0 given) In [17]: import new In [21]: f.show2 = new.instancemethod(show2, f) In [22]: f.show2() I'm still a foo I hope that makes sense to someone; I had to see it about 10 times myself before a light bulb went on. (This is number 10 :-) More info here and in the references: http://www.python.org/doc/2.2.3/whatsnew/sect-rellinks.html#SECTION00032 Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] question about classes and atributes
Thank you folks, for your excellent answers. This is really a fantastic place to learn python :-) __ LLama Gratis a cualquier PC del Mundo. Llamadas a fijos y móviles desde 1 céntimo por minuto. http://es.voice.yahoo.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] question about classes and atributes
Kent Johnson [EMAIL PROTECTED] wrote Alan Gauld wrote: I'm not sure about adding methods at run time, I've never Sure it works: In [1]: class foo(object): pass ...: In [4]: def show(self): print Hi, I'm a foo In [5]: foo.show=show In [6]: f.show() Hi, I'm a foo Cool! I'm constantly amazed at the power and simplicity of Python. More advanced explanation: Yes, it makes sense when its explained. But I'd never have intuitively thought of that! Thanks for the info Kent. Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor