Python detailed class classes by __slots__ the properties of the class instance object (vii) __python

Source: Internet
Author: User

After we have created an instance from a class, we can still add attributes to the instance, but these attributes belong only to this instance. Sometimes, we can need to restrict the properties of class instance object, then we should use the _ _slots _ _ Property in class. _ _ Slots_ _ property for a tuple, only the properties that appear in this tuple can be used by class instances.

class Person (object):
    __slots__ = (' name ', ' age ', ' weight ')
    def __init__ (self, name, age, weight):
        Self.name = name
        Self.age = age
        self.weight = weight
Bruce = Person ("Bruce", 25,60)        
print ('%s is%d years Old and he weights%s "% (Bruce.name, bruce.age,bruce.weight))
Bruce.tall = 180
Bruce years old and he weights 60
---------------------------------------------------------------------------
attributeerror                            traceback ( Most recent call last)
<ipython-input-45-6d049f8dbc1b> in <module> ()
      7 Bruce = person ("Bruce",  25,60)
      8 print ("%s is%d years old and he weights%s"% (Bruce.name, bruce.age,bruce.weight))
----> 9 bruce.tall = 180

attributeerror: ' Person ' object has no attribute ' tall '


After the person class instantiation, Bruce can not add new attributes,_ _ Slots_ _ Properties for a tuple property assignment, only the properties that appear in this tuple can be used by class instances _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ A literal property only works for an instance of the current class, does not work on an inherited subclass instance, does not restrict the inherited subclass instantiation, and then adds a new property

Class Human (object):
    __slots__ = (' name ', ' age ', ' weight ')
class person (human):
    #__slots__ = (' name ', ' age ' , "Weight")
    def __init__ (self, name, age, Weight):
        self.name = name
        Self.age = age
        self.weight = weight< C7/>bruce = Person ("Bruce", 25,60)        
print ("%s are%d years old and he weights%s"% (Bruce.name, bruce.age,bruce.weight))
Bruce.tall = 180

Bruce years old and he weights 60

If the subclass itself also has _ _ slots_ _ property, the subclass of the property is its own _ _ _ _ _ _ Slots _ and the parent class _ _ Slots_ _

Class Human (object):
    __slots__ = ("Tall")
class Person (human):
    __slots__ = ("name", "Age", "weight")
    def __init__ (self, name, age, Weight):
        self.name = name
        Self.age = age
        self.weight = weight
Bruce = person ("Bruce", 25,60)        
Print ("%s is%d years old and he weights%s"% (Bruce.name, bruce.age,bruce.weight))
Bruce.tall = 180
print ('%s is %d years old and him weights%s and he\ ' tall is%s '% (Bruce.name, bruce.age,bruce.weight,bruce.tall))
Bruce.appeara nce = ' Handsome '
Bruce is years old and he weights are years old and he weights a and he's
tall is 180
---------------------------------------------------------------------------
attributeerror                            traceback ( Most recent call last)
<ipython-input-49-617f9b7100f2> in <module> ()
     bruce.tall = 180
     13 Print ("%s is%d years old and he weights%s" and he\ ' s tall is%s '% (Bruce.name, Bruce.age,bruce.weight,bruce.tall))
-- -> bruce.appearance = ' o,no '

attributeerror: ' Person ' object has no attribute ' appearance '
The variable properties within the tuple of the parent class and subclass limit can be generated by the call. Appearance is not in the tuple list of subclasses that are no longer inherited by the parent class, and cannot be generated
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.