473,386 Members | 1,821 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

array in class

class A:
this_is_original_variable_only_for_one_inctance = 0

def __init__(self, v):
self.this_is_original_variable_only_for_one_inctan ce = v
class B:
this_is_common_for_all_instances = []

def __init__(self, v):
self.this_is_common_for_all_instances.append(v)
----------------
now I can create some instances of B, but all of them have the same
array, why

if I use append for b1.this_is_common_for_all_instances, the b2, b3
will have been changed that array.
(where bi is instance of B)
but changes on this_is_original_variable_only_for_one_inctance works
fine

why it does that?
and how create array in class - normal array, "private variable"
Jun 27 '08 #1
2 1398
alefajnie wrote:
class B:
this_is_common_for_all_instances = []

def __init__(self, v):
self.this_is_common_for_all_instances.append(v)
----------------
now I can create some instances of B, but all of them have the same
array, why
Because you didn't reassign the attribute
'this_is_common_for_all_instances', but appended to it.
and how create array in class - normal array, "private variable"
1) it's called a list, not an array
2) you do that in the __init__ method: self.blabla = []
3) still, it won't be a "private" attribute, just an instance attribute
Jun 27 '08 #2
alefajnie wrote:
class A:
this_is_original_variable_only_for_one_inctance = 0

def __init__(self, v):
self.this_is_original_variable_only_for_one_inctan ce = v
class B:
this_is_common_for_all_instances = []

def __init__(self, v):
self.this_is_common_for_all_instances.append(v)
----------------
now I can create some instances of B, but all of them have the same
array, why

if I use append for b1.this_is_common_for_all_instances, the b2, b3
will have been changed that array.
(where bi is instance of B)
but changes on this_is_original_variable_only_for_one_inctance works
fine

why it does that?
and how create array in class - normal array, "private variable"
BOTH are at first class-wide. The difference is that the list is mutable,
and thus you alter the one list, whereas a number is not - and instead you
rebind a new object under the name now in the instance itself.

You need to understand that the way you "declared" the variables above is
NOT the way instance variables work in python. It can serve as neat trick
to initialize variables to common values - but *only* if you really know
the semantics.

Instead, the idiom for creating instance-variables (that you wrongly
called "private") is this:

class Foo(object):
def __init__(self, some_value):
self.some_var = some_value

The reason why the above seems to work is that a lookup of a name in python
on an instance is done like this:

if hasattr(self, name):
return getattr(self, name)
else if hasattr(self.__class__, name):
return getattr(self.__class__, name)

That is the reason why

class Foo(object):
bar = 10

def __init__(self):
print self.bar

sees the 10 - it will first lookup on self, fail, and then lookup on Foo,
and succedd.

However, the assignment is *always* on the instance, or spelled out like
this:

setattr(self, name, value)

Thus

self.name = 20

will create a new variable named "name" with the object it points to being
20.

There are some minor details here regarding the descriptor protocol and
such, but these aren't important for now.

So - stop "declaring" variables if you want them to be per-instance. Use the
above mentioned idiom of setting them on self inside __init__

Diez
Jun 27 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: its me | last post by:
Let's say I have a class of people... Public Class People Public Sex as String Public Age as int Public Name as string end class And I declare an array of this class...
6
by: Buddy Ackerman | last post by:
I created a simple class: Public Class MyTestClass Public Test() As String End Class I tried to assign some values to the array Test() and display them like this:
14
by: Gianni Mariani | last post by:
Does anyone know if this is supposed to work ? template <unsigned N> int strn( const char str ) { return N; } #include <iostream>
3
by: michi | last post by:
Hello, I need to initialize a 2 dimensional square arrays of structures. The size of array I get from the user. I can do one-dimensional array, but I don't know how to specify the size of array...
9
by: justanotherguy63 | last post by:
Hi, I am designing an application where to preserve the hierachy and for code substitability, I need to pass an array of derived class object in place of an array of base class object. Since I...
7
by: Rade | last post by:
Please have a look at the following program: #include <iostream> template <const int array, size_t index> class ArrayIndex { public: static const int value = array; };
4
by: Armand | last post by:
Hi Guys, I have a set of array that I would like to clear and empty out. Since I am using "Array" not "ArrayList", I have been struggling in finding the solution which is a simple prob for those...
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
17
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
6
by: npankey | last post by:
I've started experimenting with template metaprogramming in a small project of mine. What I'm trying to accomplish is to generate a static array of templated objects that get specialized based on...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
0
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
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,...
0
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...
0
jinu1996
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 using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.