By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,675 Members | 2,255 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,675 IT Pros & Developers. It's quick & easy.

Difference between __init__ (again) and nothing ...

P: n/a
What's the difference between using __init__ and using nothing,
as the examples below.

<Python-1>
class cpu:
PC = 4
<Python-2>
class cpu:
def __init__:
self.PC = 4

thanks,
Stef Mientki
Jan 2 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
In <da***************************@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
Jan 2 '07 #2

P: n/a
Marc 'BlackJack' Rintsch wrote:
In <da***************************@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`.
thanks Marc,

Oh so obvious, why didn't I discovered that myself ;-)

cheers,
Stef
Jan 2 '07 #3

P: n/a
Stef Mientki a écrit :
Marc 'BlackJack' Rintsch wrote:
>In <da***************************@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__:
def __init__(self):
>> self.PC = 4
By convention, ALL_UPPER names have a 'symbolic constant' semantic.
Since Python is a very 'free' language (no attribute access restriction,
no symbolic constants etc), it *strongly* relies on conventions.

>>
This is an *instance attribute* which is set in every instance of `cpu`.
thanks Marc,

Oh so obvious, why didn't I discovered that myself ;-)
Perhaps because it may not be that obvious at first sight ?-)

(that is, until you really understand Python's object model, which is
really different from most mainstream OOPLs object models...)
Jan 2 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.