Re: [Tutor] Class vs. instance
On 18/01/12 02:13, Stayvoid wrote: class A: def __init__(self, data): self.data = data print self.data I'm trying to understand this function-like syntax: A('foo').__init__(42) You would not normally call any method with a double underscore pre/poist fix because they are special methods called by Python itself. Thus when you do A('foo') Python actually calls two special methods on class A. First it calls the __new__() method to create an instance of A then it calls __init__() with 'foo' as argument to initialise that instance. You don't need to call init() directly. In fact it may even cause problems if you initialise a class twice. So when you do A('foo').__init__(42) You actually do 3 things: First you create a new instance of A, then you initialise it with 'foo' then you initialise it again with 42. In this case no harm is done because the init)() method just does a double assignment, losing the initial value. But if you were storing the data values in a lkist you would wind up with two values instead of one, which may not be a good thing. A(12).data Here you create another instance of A and initialise it with 12 then you access its data attribute. If you do this in the interpreter the value of data will be printed, if you do it in a program nothing will happen. In both iof the cases above the newly created instances will be garbage collected since they were not assigned to any variable. What are we actually calling this way? You call the constructor __new__(), the initialiser __init__() and you access a data item which calls the accessor __getattr__() Are there any other ways to get the same result? It depends how you define 'the same results'. The same end state can be achieved in several ways. The same methods can be called in several ways, for example you can call init via the class: anAinstance = A('foo') A.__init__(anAinstance, 42) But in general all of these are a bad idea outside of a class/method definition. Don't do it. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Class vs. instance
On 01/17/2012 09:13 PM, Stayvoid wrote: Hello! Here is another one. class A: def __init__(self, data): self.data = data print self.data I'm trying to understand this function-like syntax: A('foo').__init__(42) A(12).data What are we actually calling this way? Are there any other ways to get the same result? The first line creates an instance of class A, then immediately calls the method __init__() as though it were a normal function. It then discards the object. You should only call __init__() from within another class' __init__(). It's called automatically when an object is created; leave it at that. You also usually want to keep the instance around, and use it more than once. Otherwise you could just use ordinary functions and dispense with the confusion. A(12) creates an object, then you reference the data attribute of that object, then you throw them both out. Not much use there either. Try something like: obj= A(49) print obj.data obj2 = A("artichoke") obj.data = 99 print obj.data print obj2.data Two more things. Remove the print statement from methods like __init__(), unless it's just for debugging purposes. And add a base class of object to your class definition, so that a new-style class is created. When you get to more advanced usage, it'll make a difference, and you might as well use the version of class that'll still work in Python 3.x. class A(object): . -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Class vs. instance
Hello! Here is another one. class A: def __init__(self, data): self.data = data print self.data I'm trying to understand this function-like syntax: A('foo').__init__(42) A(12).data What are we actually calling this way? Are there any other ways to get the same result? Cheers. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Class vs. instance
Thanks. I totally get it now. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Class vs. instance
On Sun, Jan 1, 2012 at 8:40 PM, Stayvoid wrote: > Hi there! > class Sample: def method(self): pass > Sample().method() > > What's the difference between class __main__.Sample and > __main__.Sample instance? > Why should I write "Sample().method" instead of "Sample.method"? > The difference can be illustrated as such: >>> Sample().method > >>> Sample().method() >>> Sample.method >>> Sample.method() Traceback (most recent call last): File "", line 1, in TypeError: unbound method method() must be called with Sample instance as first argument (got nothing instead) >>> That is, the difference between the methods is that the accessed through the instance is also attached to that instance. It will automagically get Sample() passed to it as its first argument (that would be self). The one attached to the class is unbound, which means that you can do this: >>> Sample.method(Sample()) >>> With any Sample instance, of course. This exposes a bit of syntax sugar in python and how classes are really implemented, essentially the fact that, if "a" is a sample instance, a.method(arg1, arg2, arg3) is actually just Sample.method(a, arg1, arg2, arg3) HTH, Hugo ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor