array in class

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • alefajnie

    array in class

    class A:
    this_is_origina l_variable_only _for_one_inctan ce = 0

    def __init__(self, v):
    self.this_is_or iginal_variable _only_for_one_i nctance = v


    class B:
    this_is_common_ for_all_instanc es = []

    def __init__(self, v):
    self.this_is_co mmon_for_all_in stances.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_comm on_for_all_inst ances, the b2, b3
    will have been changed that array.
    (where bi is instance of B)
    but changes on this_is_origina l_variable_only _for_one_inctan ce works
    fine

    why it does that?
    and how create array in class - normal array, "private variable"
  • Marco Mariani

    #2
    Re: array in class

    alefajnie wrote:
    class B:
    this_is_common_ for_all_instanc es = []
    >
    def __init__(self, v):
    self.this_is_co mmon_for_all_in stances.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_instan ces', 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

    Comment

    • Diez B. Roggisch

      #3
      Re: array in class

      alefajnie wrote:
      class A:
      this_is_origina l_variable_only _for_one_inctan ce = 0
      >
      def __init__(self, v):
      self.this_is_or iginal_variable _only_for_one_i nctance = v
      >
      >
      class B:
      this_is_common_ for_all_instanc es = []
      >
      def __init__(self, v):
      self.this_is_co mmon_for_all_in stances.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_comm on_for_all_inst ances, the b2, b3
      will have been changed that array.
      (where bi is instance of B)
      but changes on this_is_origina l_variable_only _for_one_inctan ce 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

      Comment

      Working...