Skip to content

Instantly share code, notes, and snippets.

@feiji110
Last active April 12, 2020 04:49
Show Gist options
  • Save feiji110/a72bd5e90c800df2887d8f746f90798d to your computer and use it in GitHub Desktop.
Save feiji110/a72bd5e90c800df2887d8f746f90798d to your computer and use it in GitHub Desktop.
200412命名元组,私有属性的getter与setter,以及只读属性的设置

私有属性的getter与setter,以及只读属性的设置

class screen():
    @property #私有属性的getter
    def height(self):
        return self.__height
    @height.setter #私有属性的setter
    def height(self,heights):
        self.__height = heights
    @property
    def width(self):
        return self.__width
    @width.setter
    def width(self,widths):
        self.__width = widths
    @property #只读属性
    def resolution(self):
        return self.__width*self.__height
s = screen()
s.height = 100
s.width = 200
print(s.resolution)#20000

不用命名元组

s = ('Jack',21 ,'male','12213123@qq.com')
name, age, sex, email  = range(4) 
print(s[name])#Jack
### 命名元组是元组的子类
````python
from collections import namedtuple
Student = namedtuple('Student',['first_name','last_name','grade'])# (类名,列表)
astudent = Student('Lisa','Simpson','A')
print(type(Student))# <class 'type'>
print(type(screen))# <class 'type'>
print(type(astudent))# <class '__main__.Student'>
print(astudent.first_name)#'Lisa'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment