python中property的简单应用场景





多注意看最后的两个print,一个是name,一个是name2
当然可以再增加个name3

原文地址:http://blog.51cto.com/13750690/2296187

时间: 2024-08-02 00:23:11

python中property的简单应用场景的相关文章

python中@property的使用

python中@property的使用 @property的作用是把一个getter方法变成属性,@xxx.setter把setter方法变成属性赋值. #coding:utf-8 class Screen(object): @property def width(self): return self._width @width.setter def width(self,value): if not isinstance(value,int): raise ValueError('width

python 中@property的使用

从14年下半年开始接触到python,自学了一段时间,后又跟别人学习了下,把基础知识基本上学过了.忽然感觉python不可能这么简单吧,就这么点东西?后来看了下书,发现还有很多的高级部分.连续看了两天,把装饰符@看了下,记录下. @装饰符的作用就是类里的方法变成属性使用,比直接调用方法要直接简单 直接上代码(没有@的): 1 class Student(object): 2 def get_age(self): 3 return self.__age 4 def set_age(self,age

python 中面向对象编程简单总结3--定制类

声明:资源来自慕课网python学习课程,以下只是个人学习总结,仅供参考 1.Python类的特殊方法 特征:以  __ 开头并结尾的方法,比如用于print的__str__() , __getattr__(),__setattr__()等   不需要在代码中直接调用, Python的某些函数和操作符会自动调用. 可以自己定制实现,如__str__()方法 class Person(object): def __init__(self,name,gender): self.name = name

python中property的使用

记得在cocoa中也有property的概念,python中的property与cocoa里的property好像是差不多的.下面是它的两种用法. 用法一: class test(object): def __init__(self): print "init is calling" self.a=100 def getX(self): print "call getX" return self.a def setX(self,value): print "

python 中面向对象编程简单总结2

1.python中继承的特点: (1)总是从一个类继承,默认为object类 (2)不要忘记调用super.__init__方法来初始化父类的方法 def __init__(self,args): super(Subclass,self).__init__(args) pass 简单例子 class Person(object): def __init__(self,name,gender): self.name = name self.gender = gender class Student

python中@property的作用和getter setter的解释

@property作用: python的@property是python的一种装饰器,是用来修饰方法的. 我们可以使用@property装饰器来创建只读属性,@property装饰器会将方法转换为相同名称的只读属性,可以与所定义的属性配合使用,这样可以防止属性被修改. 1.修饰方法,让方法可以像属性一样访问. class DataSet(object): @property def method_with_property(self): ##含有@property return 15 def m

python中的类简单讲解

类似其它的语言, Python 中的函数使用小括号( () )调用.函数在调用之前必须先定义.如果函数中没有 return 语句, 就会自动返回 None 对象.      Python 是通过引用调用的. 这意味着函数内对参数的改变会影响到原始对象.不过事实上只有可变对象会受此影响, 对不可变对象来说, 它的行为类似按值调用. 如何定义函数 def function_name([arguments]): "optional documentation string" function

Python中property属性详解

1. 什么是property属性 一种用起来像是使用的实例属性一样的特殊属性,可以对应于某个方法 # ############### 定义 ############### class Foo: def func(self): pass # 定义property属性 @property def prop(self): pass # ############### 调用 ############### foo_obj = Foo() foo_obj.func() # 调用实例方法 foo_obj.p

python中通过selenium简单操作及元素定位

浏览器的简单操作 # 导入webdriver模块 # 创建driver对象,指定Chrome浏览器 driver = webdriver.Chrome() # 窗口最大化 driver.maximize_window() # 访问百度 driver.get("http://baidu.com") driver.get("http://sina.com") # 后退 driver.back() # 前进 driver.forward() # 刷新 driver.ref