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 3 1056
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
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
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...) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: user |
last post by:
I have gotten properties to respond correctly, but
when I try to do it in __init__:
class foo:
def getter(self):
return "hello"
def __init__(self):
|
by: achan |
last post by:
As I was trying to create a metaclass, I found out that __init__ defined in
it does not initializes at all:
class CMeta(type):
def __new__(cls, ClassName, BaseClass, ClassDict):
def...
|
by: Jim Jewett |
last post by:
Normally, I expect a subclass to act in a manner consistent
with its Base classes. In particular, I don't expect to
*lose* any functionality, unless that was the whole point of
the subclass. ...
|
by: przemas_r |
last post by:
Hello everybody.
I've found python's behaviour I can't understand. Namely the way
os.system () works depending if it's being run in Thread's run function
or run in 'normal' way.
>>> os.system...
|
by: sophie_newbie |
last post by:
OK this might seem like a retarded question, but what is the difference
between a library and a module?
If I do:
import string
am I importing a module or a library?
And if i do...
|
by: John Salerno |
last post by:
I've been reading up on them, but I don't quite understand how they
differ in practice. I know how each is implemented, and from C# I
already know what a static method is. But I won't assume that...
|
by: John Salerno |
last post by:
Let's say I'm making a game and I have this base class:
class Character(object):
def __init__(self, name, stats):
self.name = name
self.strength = stats
self.dexterity = stats...
|
by: Alan Isaac |
last post by:
I am probably confused about immutable types.
But for now my questions boil down to these two:
- what does ``tuple.__init__`` do?
- what is the signature of ``tuple.__init__``?
These...
|
by: Erik Lind |
last post by:
I'm new to Python, and OOP. I've read most of Mark Lutz's book and more
online and can write simple modules, but I still don't get when __init__
needs to be used as opposed to creating a class...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
| |