Difference between __init__ (again) and nothing ...

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Tue Jan 2 06:03:57 EST 2007


In <da749$459a380d$d443bb3a$16845 at news.speedlinq.nl>, Stef Mientki wrote:

> What's the difference between using __init__ and using nothing,
> as the examples below.
> 
> class cpu:
>    PC = 4

This is a *class attribute*.  It's the same for all instances of `cpu`.

> class cpu:
>    def __init__:
>      self.PC = 4

This is an *instance attribute* which is set in every instance of `cpu`.

In [8]: class CPU_1:
   ...:     PC = 4
   ...:

In [9]: class CPU_2:
   ...:     def __init__(self):
   ...:         self.PC = 4
   ...:

In [10]: a = CPU_1()

In [11]: b = CPU_1()

In [12]: a.PC, b.PC
Out[12]: (4, 4)

In [13]: CPU_1.PC = 3.5

In [14]: a.PC, b.PC
Out[14]: (3.5, 3.5)

In [15]: c = CPU_2()

In [16]: d = CPU_2()

In [17]: c.PC, d.PC
Out[17]: (4, 4)

In [18]: c.PC = 3.5

In [19]: c.PC, d.PC
Out[19]: (3.5, 4)

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list