由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
_Python版 - setter and getter
相关主题
calculate an integral in python (转载)@synthesize的问题
damn! just found some more "secrets" INSIDE "this"请教面试时的代码规范
Other languages have "variables", Python has "names"说一下学术派的代码(java)
如何用 Python 修改 mp3 文件的音乐标题Re: 你为什么写public?
solve integral eq. embeeded with another integral eq. (转载)Object C问题请教
问个Python getter setter的问题那位大侠能介绍一下 property in c# class的使用经验
Python Browsermob Proxy Library on mac issueHelp
Amazon电话二面Simple question
相关话题的讨论汇总
话题: diameter话题: getter话题: setter话题: self话题: planet
1 (共1页)
i*****f
发帖数: 578
1
class Planet:
@property
def diameter(self):
# a getter
return self.__diameter
@diameter.setter
def diameter(self, diameter):
# a setter
if diameter<0:
raise ValueError("impossible")
self.__diameter = diameter
def __init__(self, diameter):
self.diameter = diameter
mars = Planet(100)
print mars.diameter
mars.diameter = -10 # ValueError
i*****f
发帖数: 578
2
Looks somewhat weired compared to other language setter/getter

【在 i*****f 的大作中提到】
: class Planet:
: @property
: def diameter(self):
: # a getter
: return self.__diameter
: @diameter.setter
: def diameter(self, diameter):
: # a setter
: if diameter<0:
: raise ValueError("impossible")

m*****g
发帖数: 54
3
a better version
class Planet:
def getter(self):
return self.__diameter
def setter(self, value):
self.__diameter = value
diameter = property(getter, setter)
instance = Planet()
print(Planet.diameter) # call getter
instance.diameter = 1000 # cal setter

【在 i*****f 的大作中提到】
: class Planet:
: @property
: def diameter(self):
: # a getter
: return self.__diameter
: @diameter.setter
: def diameter(self, diameter):
: # a setter
: if diameter<0:
: raise ValueError("impossible")

i*****f
发帖数: 578
4
谢谢补充!

【在 m*****g 的大作中提到】
: a better version
: class Planet:
: def getter(self):
: return self.__diameter
: def setter(self, value):
: self.__diameter = value
: diameter = property(getter, setter)
: instance = Planet()
: print(Planet.diameter) # call getter
: instance.diameter = 1000 # cal setter

1 (共1页)
相关主题
Simple questionsolve integral eq. embeeded with another integral eq. (转载)
传递一个object reference,如何防止更改object?问个Python getter setter的问题
请教关于使用map和fieldsPython Browsermob Proxy Library on mac issue
Object C问题请教 (转载)Amazon电话二面
calculate an integral in python (转载)@synthesize的问题
damn! just found some more "secrets" INSIDE "this"请教面试时的代码规范
Other languages have "variables", Python has "names"说一下学术派的代码(java)
如何用 Python 修改 mp3 文件的音乐标题Re: 你为什么写public?
相关话题的讨论汇总
话题: diameter话题: getter话题: setter话题: self话题: planet