Re: Inheritance Question

Discussion in 'Python' started by Dave Angel, Oct 18, 2012.

  1. Dave Angel

    Dave Angel Guest

    On 10/18/2012 10:10 AM, Jeff Jeffries wrote:
    > Hello everybody
    >
    > When I set "AttributeChanges" in my example, it sets the same value for all
    > other subclasses. Can someone help me with what the name of this behavior
    > is (mutable class global?) ? .... I don't know any keywords... having
    > trouble googling it
    >


    I can't understand your code or what you're trying to do with it, but
    maybe i can help anyway. Incidentally, putting code in an attachment
    will hide it from many users of this mailing list. Just paste it inline
    in your message, and make sure your message is composed as text, not html.


    Attributes can be attached to the class or to the instance. Those
    attached to the class are shared among all instances that don't hide
    them by having instance attributes of the same name.

    Any attribute bound in an instance method is specific to that instance.
    Attributes bound in the class itself belong to the class.

    class MyClass:
    classAttr1 = 42 #this is a class attribute
    classAttr2 = "will be masked" #also this
    def __init__(self):
    self.instance_attr = "each instance gets its own"
    self.classAttr2 = "this makes an instance attribute of the same
    name"

    def test(self):
    print self.classAttr1 #prints 42
    print self.classAttr2 #prints this makes an ...
    print MyClass.classAttr2 #prints will be masked

    a = MyClass()
    a.test()




    --

    DaveA
     
    Dave Angel, Oct 18, 2012
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. maxw_cc
    Replies:
    1
    Views:
    3,160
    Martijn van Steenbergen
    Dec 21, 2003
  2. cppsks
    Replies:
    0
    Views:
    834
    cppsks
    Oct 27, 2004
  3. karthikbalaguru
    Replies:
    9
    Views:
    1,055
  4. Daniel Pitts
    Replies:
    27
    Views:
    1,928
    Mike Schilling
    Feb 27, 2008
  5. johnsonlau
    Replies:
    1
    Views:
    784
    Kai-Uwe Bux
    Jul 21, 2008
Loading...

Share This Page