Tôi muốn một cuộc gọi thuộc tính như object.x
trả về kết quả của một số phương thức, giả sử object.other.other_method[]
. Tôi có thể làm cái này như thế nào?
EDIT: Tôi đã hỏi sớm một chút: Có vẻ như tôi có thể làm điều này với
object.__dict__['x']=object.other.other_method[]
Đây có phải là một cách ok để làm điều này?
hỏi ngày 2 tháng 7 năm 2010 lúc 14:51Jul 2, 2010 at 14:51
1
Sử dụng người trang trí tài sản
class Test[object]: # make sure you inherit from object
@property
def x[self]:
return 4
p = Test[]
p.x # returns 4
Mucking với __dict__ là bẩn, đặc biệt là khi @Property có sẵn.
Chhantyal
11,5K7 Huy hiệu vàng48 Huy hiệu bạc75 Huy hiệu đồng7 gold badges48 silver badges75 bronze badges
Đã trả lời ngày 2 tháng 7 năm 2010 lúc 14:55Jul 2, 2010 at 14:55
Donald Minerdonald MinerDonald Miner
37.9K8 Huy hiệu vàng90 Huy hiệu bạc117 Huy hiệu đồng8 gold badges90 silver badges117 bronze badges
2
Có một cái nhìn về chức năng thuộc tính tích hợp.
Đã trả lời ngày 2 tháng 7 năm 2010 lúc 14:53Jul 2, 2010 at 14:53
muksiemksiemuksie
Huy hiệu vàng 12K11 gold badge18 silver badges14 bronze badges
Sử dụng
class Test[object]: # make sure you inherit from object
@property
def x[self]:
return 4
p = Test[]
p.x # returns 4
0//docs.python.org/library/functions.html#property
class MyClass[object]:
def __init__[self, x]:
self._x = x
def get_x[self]:
print "in get_x: do something here"
return self._x
def set_x[self, x]:
print "in set_x: do something"
self._x = x
x = property[get_x, set_x]
if __name__ == '__main__':
m = MyClass[10]
# getting x
print 'm.x is %s' % m.x
# setting x
m.x = 5
# getting new x
print 'm.x is %s' % m.x
Đã trả lời ngày 2 tháng 7 năm 2010 lúc 15:00Jul 2, 2010 at 15:00
Gary Kerrgary KerrGary Kerr
12.8k4 Huy hiệu vàng48 Huy hiệu bạc51 Huy hiệu Đồng4 gold badges48 silver badges51 bronze badges
Điều này sẽ chỉ gọi
class Test[object]: # make sure you inherit from object
@property
def x[self]:
return 4
p = Test[]
p.x # returns 4
1 một lần khi nó được tạoobject.__dict__['x']=object.other.other_method[]
Thay vào đó bạn có thể làm điều này
object.x = property[object.other.other_method]
Gọi
class Test[object]: # make sure you inherit from object
@property
def x[self]:
return 4
p = Test[]
p.x # returns 4
1 mỗi khi object.x
được truy cậpTất nhiên bạn không thực sự sử dụng
class Test[object]: # make sure you inherit from object
@property
def x[self]:
return 4
p = Test[]
p.x # returns 4
4 làm tên biến, phải không?Đã trả lời ngày 2 tháng 7 năm 2010 lúc 15:24Jul 2, 2010 at 15:24
John La Rooyjohn La RooyJohn La Rooy
Phù bằng vàng 287K5151 gold badges359 silver badges500 bronze badges
1
Truy cập các thuộc tính và phương thức của một lớp trong một lớp khác được thực hiện bằng cách chuyển đối tượng của một lớp sang một lớp khác. Giải thích bằng ví dụ được đưa ra dưới đây:
5 class Test[object]: # make sure you inherit from object
@property
def x[self]:
return 4
p = Test[]
p.x # returns 4
03class Test[object]: # make sure you inherit from object
@property
def x[self]:
return 4
p = Test[]
p.x # returns 4
class Test[object]: # make sure you inherit from object
@property
def x[self]:
return 4
p = Test[]
p.x # returns 4
7class MyClass[object]:
def __init__[self, x]:
self._x = x
def get_x[self]:
print "in get_x: do something here"
return self._x
def set_x[self, x]:
print "in set_x: do something"
self._x = x
x = property[get_x, set_x]
if __name__ == '__main__':
m = MyClass[10]
# getting x
print 'm.x is %s' % m.x
# setting x
m.x = 5
# getting new x
print 'm.x is %s' % m.x
6 class Test[object]: # make sure you inherit from object
@property
def x[self]:
return 4
p = Test[]
p.x # returns 4
06class MyClass[object]:
def __init__[self, x]:
self._x = x
def get_x[self]:
print "in get_x: do something here"
return self._x
def set_x[self, x]:
print "in set_x: do something"
self._x = x
x = property[get_x, set_x]
if __name__ == '__main__':
m = MyClass[10]
# getting x
print 'm.x is %s' % m.x
# setting x
m.x = 5
# getting new x
print 'm.x is %s' % m.x
8class MyClass[object]:
def __init__[self, x]:
self._x = x
def get_x[self]:
print "in get_x: do something here"
return self._x
def set_x[self, x]:
print "in set_x: do something"
self._x = x
x = property[get_x, set_x]
if __name__ == '__main__':
m = MyClass[10]
# getting x
print 'm.x is %s' % m.x
# setting x
m.x = 5
# getting new x
print 'm.x is %s' % m.x
9object.__dict__['x']=object.other.other_method[]
0class MyClass[object]:
def __init__[self, x]:
self._x = x
def get_x[self]:
print "in get_x: do something here"
return self._x
def set_x[self, x]:
print "in set_x: do something"
self._x = x
x = property[get_x, set_x]
if __name__ == '__main__':
m = MyClass[10]
# getting x
print 'm.x is %s' % m.x
# setting x
m.x = 5
# getting new x
print 'm.x is %s' % m.x
8class Test[object]: # make sure you inherit from object
@property
def x[self]:
return 4
p = Test[]
p.x # returns 4
11class Test[object]: # make sure you inherit from object
@property
def x[self]:
return 4
p = Test[]
p.x # returns 4
9 class Test[object]: # make sure you inherit from object
@property
def x[self]:
return 4
p = Test[]
p.x # returns 4
13Truy cập các thuộc tính và phương thức của một lớp trong một lớp khác được thực hiện bằng cách chuyển đối tượng của một lớp sang một lớp khác. Giải thích bằng ví dụ được đưa ra dưới đây:
5 class Test[object]: # make sure you inherit from object
@property
def x[self]:
return 4
p = Test[]
p.x # returns 4
03class Test[object]: # make sure you inherit from object
@property
def x[self]:
return 4
p = Test[]
p.x # returns 4
class Test[object]: # make sure you inherit from object
@property
def x[self]:
return 4
p = Test[]
p.x # returns 4
7class MyClass[object]:
def __init__[self, x]:
self._x = x
def get_x[self]:
print "in get_x: do something here"
return self._x
def set_x[self, x]:
print "in set_x: do something"
self._x = x
x = property[get_x, set_x]
if __name__ == '__main__':
m = MyClass[10]
# getting x
print 'm.x is %s' % m.x
# setting x
m.x = 5
# getting new x
print 'm.x is %s' % m.x
6 class Test[object]: # make sure you inherit from object
@property
def x[self]:
return 4
p = Test[]
p.x # returns 4
06class MyClass[object]:
def __init__[self, x]:
self._x = x
def get_x[self]:
print "in get_x: do something here"
return self._x
def set_x[self, x]:
print "in set_x: do something"
self._x = x
x = property[get_x, set_x]
if __name__ == '__main__':
m = MyClass[10]
# getting x
print 'm.x is %s' % m.x
# setting x
m.x = 5
# getting new x
print 'm.x is %s' % m.x
8class MyClass[object]:
def __init__[self, x]:
self._x = x
def get_x[self]:
print "in get_x: do something here"
return self._x
def set_x[self, x]:
print "in set_x: do something"
self._x = x
x = property[get_x, set_x]
if __name__ == '__main__':
m = MyClass[10]
# getting x
print 'm.x is %s' % m.x
# setting x
m.x = 5
# getting new x
print 'm.x is %s' % m.x
9Attributes of a class can also be accessed using the following built-in methods and functions :
0object.__dict__['x']=object.other.other_method[]
8class MyClass[object]: def __init__[self, x]: self._x = x def get_x[self]: print "in get_x: do something here" return self._x def set_x[self, x]: print "in set_x: do something" self._x = x x = property[get_x, set_x] if __name__ == '__main__': m = MyClass[10] # getting x print 'm.x is %s' % m.x # setting x m.x = 5 # getting new x print 'm.x is %s' % m.x
11class Test[object]: # make sure you inherit from object @property def x[self]: return 4 p = Test[] p.x # returns 4
9class Test[object]: # make sure you inherit from object @property def x[self]: return 4 p = Test[] p.x # returns 4
13 This function is used to access the attribute of object.class Test[object]: # make sure you inherit from object @property def x[self]: return 4 p = Test[] p.x # returns 4
0object.__dict__['x']=object.other.other_method[]
8class MyClass[object]: def __init__[self, x]: self._x = x def get_x[self]: print "in get_x: do something here" return self._x def set_x[self, x]: print "in set_x: do something" self._x = x x = property[get_x, set_x] if __name__ == '__main__': m = MyClass[10] # getting x print 'm.x is %s' % m.x # setting x m.x = 5 # getting new x print 'm.x is %s' % m.x
16class Test[object]: # make sure you inherit from object @property def x[self]: return 4 p = Test[] p.x # returns 4
9class Test[object]: # make sure you inherit from object @property def x[self]: return 4 p = Test[] p.x # returns 4
object.other.other_method[]
1 This function is used to check if an attribute exist or not.
7class Test[object]: # make sure you inherit from object @property def x[self]: return 4 p = Test[] p.x # returns 4
6class MyClass[object]: def __init__[self, x]: self._x = x def get_x[self]: print "in get_x: do something here" return self._x def set_x[self, x]: print "in set_x: do something" self._x = x x = property[get_x, set_x] if __name__ == '__main__': m = MyClass[10] # getting x print 'm.x is %s' % m.x # setting x m.x = 5 # getting new x print 'm.x is %s' % m.x
21class Test[object]: # make sure you inherit from object @property def x[self]: return 4 p = Test[] p.x # returns 4
8class MyClass[object]: def __init__[self, x]: self._x = x def get_x[self]: print "in get_x: do something here" return self._x def set_x[self, x]: print "in set_x: do something" self._x = x x = property[get_x, set_x] if __name__ == '__main__': m = MyClass[10] # getting x print 'm.x is %s' % m.x # setting x m.x = 5 # getting new x print 'm.x is %s' % m.x
9 This function is used to set an attribute. If the attribute does not exist, then it would be created.class MyClass[object]: def __init__[self, x]: self._x = x def get_x[self]: print "in get_x: do something here" return self._x def set_x[self, x]: print "in set_x: do something" self._x = x x = property[get_x, set_x] if __name__ == '__main__': m = MyClass[10] # getting x print 'm.x is %s' % m.x # setting x m.x = 5 # getting new x print 'm.x is %s' % m.x
- delattr [] - Hàm này được sử dụng để xóa một thuộc tính. Nếu bạn đang truy cập thuộc tính sau khi xóa, nó sẽ tăng lỗi, lớp không có thuộc tính nào. This function is used to delete an attribute. If you are accessing the attribute after deleting it raises error “class has no attribute”.
Các phương pháp sau được giải thích với ví dụ được đưa ra dưới đây:
class Test[object]: # make sure you inherit from object
@property
def x[self]:
return 4
p = Test[]
p.x # returns 4
5 class Test[object]: # make sure you inherit from object
@property
def x[self]:
return 4
p = Test[]
p.x # returns 4
6class Test[object]: # make sure you inherit from object
@property
def x[self]:
return 4
p = Test[]
p.x # returns 4
7class Test[object]: # make sure you inherit from object
@property
def x[self]:
return 4
p = Test[]
p.x # returns 4
8class Test[object]: # make sure you inherit from object
@property
def x[self]:
return 4
p = Test[]
p.x # returns 4
9class MyClass[object]:
def __init__[self, x]:
self._x = x
def get_x[self]:
print "in get_x: do something here"
return self._x
def set_x[self, x]:
print "in set_x: do something"
self._x = x
x = property[get_x, set_x]
if __name__ == '__main__':
m = MyClass[10]
# getting x
print 'm.x is %s' % m.x
# setting x
m.x = 5
# getting new x
print 'm.x is %s' % m.x
0class Test[object]: # make sure you inherit from object
@property
def x[self]:
return 4
p = Test[]
p.x # returns 4
7class MyClass[object]:
def __init__[self, x]:
self._x = x
def get_x[self]:
print "in get_x: do something here"
return self._x
def set_x[self, x]:
print "in set_x: do something"
self._x = x
x = property[get_x, set_x]
if __name__ == '__main__':
m = MyClass[10]
# getting x
print 'm.x is %s' % m.x
# setting x
m.x = 5
# getting new x
print 'm.x is %s' % m.x
2class Test[object]: # make sure you inherit from object
@property
def x[self]:
return 4
p = Test[]
p.x # returns 4
9class MyClass[object]:
def __init__[self, x]:
self._x = x
def get_x[self]:
print "in get_x: do something here"
return self._x
def set_x[self, x]:
print "in set_x: do something"
self._x = x
x = property[get_x, set_x]
if __name__ == '__main__':
m = MyClass[10]
# getting x
print 'm.x is %s' % m.x
# setting x
m.x = 5
# getting new x
print 'm.x is %s' % m.x
4class Test[object]: # make sure you inherit from object
@property
def x[self]:
return 4
p = Test[]
p.x # returns 4
7class MyClass[object]:
def __init__[self, x]:
self._x = x
def get_x[self]:
print "in get_x: do something here"
return self._x
def set_x[self, x]:
print "in set_x: do something"
self._x = x
x = property[get_x, set_x]
if __name__ == '__main__':
m = MyClass[10]
# getting x
print 'm.x is %s' % m.x
# setting x
m.x = 5
# getting new x
print 'm.x is %s' % m.x
6 class MyClass[object]:
def __init__[self, x]:
self._x = x
def get_x[self]:
print "in get_x: do something here"
return self._x
def set_x[self, x]:
print "in set_x: do something"
self._x = x
x = property[get_x, set_x]
if __name__ == '__main__':
m = MyClass[10]
# getting x
print 'm.x is %s' % m.x
# setting x
m.x = 5
# getting new x
print 'm.x is %s' % m.x
7class MyClass[object]:
def __init__[self, x]:
self._x = x
def get_x[self]:
print "in get_x: do something here"
return self._x
def set_x[self, x]:
print "in set_x: do something"
self._x = x
x = property[get_x, set_x]
if __name__ == '__main__':
m = MyClass[10]
# getting x
print 'm.x is %s' % m.x
# setting x
m.x = 5
# getting new x
print 'm.x is %s' % m.x
8class MyClass[object]:
def __init__[self, x]:
self._x = x
def get_x[self]:
print "in get_x: do something here"
return self._x
def set_x[self, x]:
print "in set_x: do something"
self._x = x
x = property[get_x, set_x]
if __name__ == '__main__':
m = MyClass[10]
# getting x
print 'm.x is %s' % m.x
# setting x
m.x = 5
# getting new x
print 'm.x is %s' % m.x
9object.__dict__['x']=object.other.other_method[]
0object.__dict__['x']=object.other.other_method[]
1 object.__dict__['x']=object.other.other_method[]
2class MyClass[object]:
def __init__[self, x]:
self._x = x
def get_x[self]:
print "in get_x: do something here"
return self._x
def set_x[self, x]:
print "in set_x: do something"
self._x = x
x = property[get_x, set_x]
if __name__ == '__main__':
m = MyClass[10]
# getting x
print 'm.x is %s' % m.x
# setting x
m.x = 5
# getting new x
print 'm.x is %s' % m.x
8object.__dict__['x']=object.other.other_method[]
4object.__dict__['x']=object.other.other_method[]
0object.__dict__['x']=object.other.other_method[]
1 object.__dict__['x']=object.other.other_method[]
2class MyClass[object]:
def __init__[self, x]:
self._x = x
def get_x[self]:
print "in get_x: do something here"
return self._x
def set_x[self, x]:
print "in set_x: do something"
self._x = x
x = property[get_x, set_x]
if __name__ == '__main__':
m = MyClass[10]
# getting x
print 'm.x is %s' % m.x
# setting x
m.x = 5
# getting new x
print 'm.x is %s' % m.x
8object.__dict__['x']=object.other.other_method[]
9object.x = property[object.other.other_method]
0class Test[object]: # make sure you inherit from object
@property
def x[self]:
return 4
p = Test[]
p.x # returns 4
9 object.x = property[object.other.other_method]
2object.__dict__['x']=object.other.other_method[]
1 object.__dict__['x']=object.other.other_method[]
2object.x = property[object.other.other_method]
5object.x = property[object.other.other_method]
6object.x = property[object.other.other_method]
7object.x = property[object.other.other_method]
8object.__dict__['x']=object.other.other_method[]
1 object.__dict__['x']=object.other.other_method[]
2Harsh True 1521
object.x = property[object.other.other_method]
6object.x = property[object.other.other_method]
7object.x = property[object.other.other_method]
8Harsh True 1525
object.x = property[object.other.other_method]
6Harsh True 1527
Harsh True 1528
Harsh True 1529
4 9 90
object.__dict__['x']=object.other.other_method[]
1 object.__dict__['x']=object.other.other_method[]
2object.x = property[object.other.other_method]
5object.x = property[object.other.other_method]
6Harsh True 1527
object.x = property[object.other.other_method]
84 9 97
4 9 98
4 9 99
4 9 90
Đầu ra:
Harsh True 152
Phương pháp tĩnh: Phương pháp tĩnh là một phương thức [hàm thành viên] không sử dụng đối số tự. Để khai báo một phương thức tĩnh, hãy tiến hành câu lệnh của câu lệnh @staticmethod.A static method is a method[member function] that don’t use argument self at all. To declare a static method, proceed it with the statement “@staticmethod”.
class Test[object]: # make sure you inherit from object
@property
def x[self]:
return 4
p = Test[]
p.x # returns 4
5 3 3 22
class Test[object]: # make sure you inherit from object
@property
def x[self]:
return 4
p = Test[]
p.x # returns 4
73 3 24
class Test[object]: # make sure you inherit from object
@property
def x[self]:
return 4
p = Test[]
p.x # returns 4
7class MyClass[object]:
def __init__[self, x]:
self._x = x
def get_x[self]:
print "in get_x: do something here"
return self._x
def set_x[self, x]:
print "in set_x: do something"
self._x = x
x = property[get_x, set_x]
if __name__ == '__main__':
m = MyClass[10]
# getting x
print 'm.x is %s' % m.x
# setting x
m.x = 5
# getting new x
print 'm.x is %s' % m.x
6 3 3 27
object.__dict__['x']=object.other.other_method[]
0__79class Test[object]: # make sure you inherit from object
@property
def x[self]:
return 4
p = Test[]
p.x # returns 4
9 object.x
1object.x
2object.x
3object.x
4
class Test[object]: # make sure you inherit from object
@property
def x[self]:
return 4
p = Test[]
p.x # returns 4
9object.x
6object.x
7
class Test[object]: # make sure you inherit from object
@property
def x[self]:
return 4
p = Test[]
p.x # returns 4
9 object.x
6object.other.other_method[]
0object.other.other_method[]
1
4 9 90
object.__dict__['x']=object.other.other_method[]
1 object.other.other_method[]
4object.other.other_method[]
5object.other.other_method[]
6
4 9 90
object.__dict__['x']=object.other.other_method[]
1 object.other.other_method[]
9object.__dict__['x']=object.other.other_method[]
1 object.other.other_method[]
4object.__dict__['x']=object.other.other_method[]
1 object.other.other_method[]
94 9 9
Đầu ra:
Truy cập các thuộc tính và phương thức của một lớp trong một lớp khác
Explained with the example given below :
Truy cập các thuộc tính và phương thức của một lớp trong một lớp khác được thực hiện bằng cách chuyển đối tượng của một lớp sang một lớp khác. Giải thích bằng ví dụ được đưa ra dưới đây:
class Test[object]: # make sure you inherit from object
@property
def x[self]:
return 4
p = Test[]
p.x # returns 4
5 class Test[object]: # make sure you inherit from object
@property
def x[self]:
return 4
p = Test[]
p.x # returns 4
03class Test[object]: # make sure you inherit from object
@property
def x[self]:
return 4
p = Test[]
p.x # returns 4
7class MyClass[object]:
def __init__[self, x]:
self._x = x
def get_x[self]:
print "in get_x: do something here"
return self._x
def set_x[self, x]:
print "in set_x: do something"
self._x = x
x = property[get_x, set_x]
if __name__ == '__main__':
m = MyClass[10]
# getting x
print 'm.x is %s' % m.x
# setting x
m.x = 5
# getting new x
print 'm.x is %s' % m.x
6 class Test[object]: # make sure you inherit from object
@property
def x[self]:
return 4
p = Test[]
p.x # returns 4
06class MyClass[object]:
def __init__[self, x]:
self._x = x
def get_x[self]:
print "in get_x: do something here"
return self._x
def set_x[self, x]:
print "in set_x: do something"
self._x = x
x = property[get_x, set_x]
if __name__ == '__main__':
m = MyClass[10]
# getting x
print 'm.x is %s' % m.x
# setting x
m.x = 5
# getting new x
print 'm.x is %s' % m.x
8class MyClass[object]:
def __init__[self, x]:
self._x = x
def get_x[self]:
print "in get_x: do something here"
return self._x
def set_x[self, x]:
print "in set_x: do something"
self._x = x
x = property[get_x, set_x]
if __name__ == '__main__':
m = MyClass[10]
# getting x
print 'm.x is %s' % m.x
# setting x
m.x = 5
# getting new x
print 'm.x is %s' % m.x
9object.__dict__['x']=object.other.other_method[]
0class MyClass[object]:
def __init__[self, x]:
self._x = x
def get_x[self]:
print "in get_x: do something here"
return self._x
def set_x[self, x]:
print "in set_x: do something"
self._x = x
x = property[get_x, set_x]
if __name__ == '__main__':
m = MyClass[10]
# getting x
print 'm.x is %s' % m.x
# setting x
m.x = 5
# getting new x
print 'm.x is %s' % m.x
8class Test[object]: # make sure you inherit from object
@property
def x[self]:
return 4
p = Test[]
p.x # returns 4
11class Test[object]: # make sure you inherit from object
@property
def x[self]:
return 4
p = Test[]
p.x # returns 4
9 class Test[object]: # make sure you inherit from object
@property
def x[self]:
return 4
p = Test[]
p.x # returns 4
13object.__dict__['x']=object.other.other_method[]
0class MyClass[object]:
def __init__[self, x]:
self._x = x
def get_x[self]:
print "in get_x: do something here"
return self._x
def set_x[self, x]:
print "in set_x: do something"
self._x = x
x = property[get_x, set_x]
if __name__ == '__main__':
m = MyClass[10]
# getting x
print 'm.x is %s' % m.x
# setting x
m.x = 5
# getting new x
print 'm.x is %s' % m.x
8class Test[object]: # make sure you inherit from object
@property
def x[self]:
return 4
p = Test[]
p.x # returns 4
16class Test[object]: # make sure you inherit from object
@property
def x[self]:
return 4
p = Test[]
p.x # returns 4
9 object.other.other_method[]
1class Test[object]: # make sure you inherit from object
@property
def x[self]:
return 4
p = Test[]
p.x # returns 4
7class MyClass[object]:
def __init__[self, x]:
self._x = x
def get_x[self]:
print "in get_x: do something here"
return self._x
def set_x[self, x]:
print "in set_x: do something"
self._x = x
x = property[get_x, set_x]
if __name__ == '__main__':
m = MyClass[10]
# getting x
print 'm.x is %s' % m.x
# setting x
m.x = 5
# getting new x
print 'm.x is %s' % m.x
6 class Test[object]: # make sure you inherit from object
@property
def x[self]:
return 4
p = Test[]
p.x # returns 4
21class MyClass[object]:
def __init__[self, x]:
self._x = x
def get_x[self]:
print "in get_x: do something here"
return self._x
def set_x[self, x]:
print "in set_x: do something"
self._x = x
x = property[get_x, set_x]
if __name__ == '__main__':
m = MyClass[10]
# getting x
print 'm.x is %s' % m.x
# setting x
m.x = 5
# getting new x
print 'm.x is %s' % m.x
8class MyClass[object]:
def __init__[self, x]:
self._x = x
def get_x[self]:
print "in get_x: do something here"
return self._x
def set_x[self, x]:
print "in set_x: do something"
self._x = x
x = property[get_x, set_x]
if __name__ == '__main__':
m = MyClass[10]
# getting x
print 'm.x is %s' % m.x
# setting x
m.x = 5
# getting new x
print 'm.x is %s' % m.x
9Các
object.__dict__['x']=object.other.other_method[]
0class Test[object]: # make sure you inherit from object
@property
def x[self]:
return 4
p = Test[]
p.x # returns 4
34 class MyClass[object]:
def __init__[self, x]:
self._x = x
def get_x[self]:
print "in get_x: do something here"
return self._x
def set_x[self, x]:
print "in set_x: do something"
self._x = x
x = property[get_x, set_x]
if __name__ == '__main__':
m = MyClass[10]
# getting x
print 'm.x is %s' % m.x
# setting x
m.x = 5
# getting new x
print 'm.x is %s' % m.x
8class Test[object]: # make sure you inherit from object
@property
def x[self]:
return 4
p = Test[]
p.x # returns 4
36class Test[object]: # make sure you inherit from object
@property
def x[self]:
return 4
p = Test[]
p.x # returns 4
5 class Test[object]: # make sure you inherit from object
@property
def x[self]:
return 4
p = Test[]
p.x # returns 4
38class Test[object]: # make sure you inherit from object
@property
def x[self]:
return 4
p = Test[]
p.x # returns 4
7class MyClass[object]:
def __init__[self, x]:
self._x = x
def get_x[self]:
print "in get_x: do something here"
return self._x
def set_x[self, x]:
print "in set_x: do something"
self._x = x
x = property[get_x, set_x]
if __name__ == '__main__':
m = MyClass[10]
# getting x
print 'm.x is %s' % m.x
# setting x
m.x = 5
# getting new x
print 'm.x is %s' % m.x
6 class Test[object]: # make sure you inherit from object
@property
def x[self]:
return 4
p = Test[]
p.x # returns 4
06class MyClass[object]:
def __init__[self, x]:
self._x = x
def get_x[self]:
print "in get_x: do something here"
return self._x
def set_x[self, x]:
print "in set_x: do something"
self._x = x
x = property[get_x, set_x]
if __name__ == '__main__':
m = MyClass[10]
# getting x
print 'm.x is %s' % m.x
# setting x
m.x = 5
# getting new x
print 'm.x is %s' % m.x
8class Test[object]: # make sure you inherit from object
@property
def x[self]:
return 4
p = Test[]
p.x # returns 4
43object.__dict__['x']=object.other.other_method[]
0class MyClass[object]:
def __init__[self, x]:
self._x = x
def get_x[self]:
print "in get_x: do something here"
return self._x
def set_x[self, x]:
print "in set_x: do something"
self._x = x
x = property[get_x, set_x]
if __name__ == '__main__':
m = MyClass[10]
# getting x
print 'm.x is %s' % m.x
# setting x
m.x = 5
# getting new x
print 'm.x is %s' % m.x
8class Test[object]: # make sure you inherit from object
@property
def x[self]:
return 4
p = Test[]
p.x # returns 4
11class Test[object]: # make sure you inherit from object
@property
def x[self]:
return 4
p = Test[]
p.x # returns 4
9 class Test[object]: # make sure you inherit from object
@property
def x[self]:
return 4
p = Test[]
p.x # returns 4
48object.__dict__['x']=object.other.other_method[]
0____28class Test[object]: # make sure you inherit from object
@property
def x[self]:
return 4
p = Test[]
p.x # returns 4
16class Test[object]: # make sure you inherit from object
@property
def x[self]:
return 4
p = Test[]
p.x # returns 4
9 class Test[object]: # make sure you inherit from object
@property
def x[self]:
return 4
p = Test[]
p.x # returns 4
53class Test[object]: # make sure you inherit from object
@property
def x[self]:
return 4
p = Test[]
p.x # returns 4
54class Test[object]: # make sure you inherit from object
@property
def x[self]:
return 4
p = Test[]
p.x # returns 4
9 class Test[object]: # make sure you inherit from object
@property
def x[self]:
return 4
p = Test[]
p.x # returns 4
56class Test[object]: # make sure you inherit from object
@property
def x[self]:
return 4
p = Test[]
p.x # returns 4
57class Test[object]: # make sure you inherit from object
@property
def x[self]:
return 4
p = Test[]
p.x # returns 4
9 class Test[object]: # make sure you inherit from object
@property
def x[self]:
return 4
p = Test[]
p.x # returns 4
59object.__dict__['x']=object.other.other_method[]
1 class Test[object]: # make sure you inherit from object
@property
def x[self]:
return 4
p = Test[]
p.x # returns 4
61object.__dict__['x']=object.other.other_method[]
1class Test[object]: # make sure you inherit from object
@property
def x[self]:
return 4
p = Test[]
p.x # returns 4
66class Test[object]: # make sure you inherit from object
@property
def x[self]:
return 4
p = Test[]
p.x # returns 4
62class Test[object]: # make sure you inherit from object
@property
def x[self]:
return 4
p = Test[]
p.x # returns 4
9 class Test[object]: # make sure you inherit from object
@property
def x[self]:
return 4
p = Test[]
p.x # returns 4
64object.__dict__['x']=object.other.other_method[]
1 object.other.other_method[]
93 3 2