Python Decorators
Moderator: mindphp, ผู้ดูแลกระดาน
- nuattawoot
- PHP VIP Members
- โพสต์: 2561
- ลงทะเบียนเมื่อ: 05/06/2017 9:34 am
Python Decorators
Python Decorators @decorators
Python decorators in classes https://www.mindphp.com/forums/viewtopic ... 83#p105184
Python decorators in Method https://www.mindphp.com/forums/viewtopic ... 25#p105186
Python decorators in classes https://www.mindphp.com/forums/viewtopic ... 83#p105184
Python decorators in Method https://www.mindphp.com/forums/viewtopic ... 25#p105186
แก้ไขล่าสุดโดย nuattawoot เมื่อ 26/09/2017 6:59 pm, แก้ไขไปแล้ว 4 ครั้ง.
First Bug Love you
- nuattawoot
- PHP VIP Members
- โพสต์: 2561
- ลงทะเบียนเมื่อ: 05/06/2017 9:34 am
Re: Python Decorators
Python decorators in classes
เป็นตังครอบคลุมไวยากรณ์ decorator และแนวคิดของ decorator (หรือ decorating) callable
Decorators เป็น syntax ซึ่งช่วยให้ไฟล์ต้นฉบับของ Python สามารถบอกได้ว่าจะทำอย่างไรกับผลลัพธ์ของ ฟังก์ชัน หรือคำสั่ง class แทนที่จะเป็นคำสั่ง Decorators หรือ function สามารถใช้งานได้ตั้งแต่ Python 2.4 และ บน Class Python 2.6
ไวยากรณ์ decorator
ไวยากรณ์ของใช้ @ character. สำหรับคำสั่งฟังก์ชันต่อไปนี้จะเทียบเท่า:
# State ก่อนกำหนด f ที่ a_decorator จะถูกใช้กับมัน
ประโยชน์ของการใช้ไวยากรณ์ decorator คือ:
ชื่อของฟังก์ชั่นจะปรากฏขึ้นเพียงครั้งเดียวในไฟล์ต้นฉบับเท่านั้น
ผู้อ่านรู้ก่อนที่คำนิยามที่ยาวพอสมควรของฟังก์ชันฟังก์ชันมัณฑนากรจะถูกนำไปใช้
ไวยากรณ์ของ decorator สำหรับคำสั่ง class เหมือนกันเว้นแต่ว่าจะใช้กับ statement ของคลาส
Bound methods
เมื่อเราเรียกเมธอด bound methods a จะถูกส่งผ่านเป็นอาร์กิวเมนต์
staticmethod()
วิธีการแบบสร้างเมธอดที่ถูกผูกไว้เมื่อเข้าถึงฟังก์ชัน
เมื่อเราเรียกวิธีคงที่เราไม่ได้รับอาร์กิวเมนต์เพิ่มเติมใด ๆ
The call() decorator
สมมติว่าเราต้องการสร้างตารางค้นหาโดยกล่าวว่าประกอบด้วยเต็มบวกจำนวน 0 ถึง n
สำหรับ n เล็กเราสามารถรับเข้ามาได้:
(5, 9)
สูตรนี้ ทำความเข้าใจง่าย เลยใช้สูตรนี้
(5, 9)
โค้ด: เลือกทั้งหมด
class Test(object):
@classmethod
def _decorator(cls, foo):
foo()
def bar(self):
pass
Test.bar = Test._decorator(Test.bar)
โค้ด: เลือกทั้งหมด
class myclass(object):
def __init__(self):
self.property = "HELLO"
@adecorator(property="GOODBYE")
def method(self):
print self.property
Decorators เป็น syntax ซึ่งช่วยให้ไฟล์ต้นฉบับของ Python สามารถบอกได้ว่าจะทำอย่างไรกับผลลัพธ์ของ ฟังก์ชัน หรือคำสั่ง class แทนที่จะเป็นคำสั่ง Decorators หรือ function สามารถใช้งานได้ตั้งแต่ Python 2.4 และ บน Class Python 2.6
ไวยากรณ์ decorator
ไวยากรณ์ของใช้ @ character. สำหรับคำสั่งฟังก์ชันต่อไปนี้จะเทียบเท่า:
# State ก่อนกำหนด f ที่ a_decorator จะถูกใช้กับมัน
โค้ด: เลือกทั้งหมด
@a_decorator
def f(...):
โค้ด: เลือกทั้งหมด
def f(...):
...
# หลังจากกำหนด f ให้ใช้ a_decorator ไป
f = a_decorator(f)
ชื่อของฟังก์ชั่นจะปรากฏขึ้นเพียงครั้งเดียวในไฟล์ต้นฉบับเท่านั้น
ผู้อ่านรู้ก่อนที่คำนิยามที่ยาวพอสมควรของฟังก์ชันฟังก์ชันมัณฑนากรจะถูกนำไปใช้
ไวยากรณ์ของ decorator สำหรับคำสั่ง class เหมือนกันเว้นแต่ว่าจะใช้กับ statement ของคลาส
Bound methods
โค้ด: เลือกทั้งหมด
class A(object):
def method(*argv):
return argv
a = A()
a.method
<bound method A.method of <A object at 0x...>>
โค้ด: เลือกทั้งหมด
a.method('an arg')
(<A object at 0x...>, 'an arg')
a.method('an arg')[0] is a
True
วิธีการแบบสร้างเมธอดที่ถูกผูกไว้เมื่อเข้าถึงฟังก์ชัน
โค้ด: เลือกทั้งหมด
class A(object):
@staticmethod
def method(*argv):
return argv
a = A()
a.method
<function method at 0x...>
โค้ด: เลือกทั้งหมด
a.method('an arg')
('an arg',)
สมมติว่าเราต้องการสร้างตารางค้นหาโดยกล่าวว่าประกอบด้วยเต็มบวกจำนวน 0 ถึง n
สำหรับ n เล็กเราสามารถรับเข้ามาได้:
โค้ด: เลือกทั้งหมด
table = [0, 1, 4, 9, 16]
len(table), table[3]
สูตรนี้ ทำความเข้าใจง่าย เลยใช้สูตรนี้
โค้ด: เลือกทั้งหมด
table = [i * i for i in range(5)]
len(table), table[3]
First Bug Love you
- nuattawoot
- PHP VIP Members
- โพสต์: 2561
- ลงทะเบียนเมื่อ: 05/06/2017 9:34 am
Re: Python Decorators
Python decorators in Method
ใน Python Method คือฟังก์ชันที่ พารามิเตอร์แรกจะอ้างอิงกับอ็อบเจ็กต์ปัจจุบัน เราสามารถสร้าง decorators วิธีการเช่นเดียวกับในการพิจารณาตัวเองในฟังก์ชัน wrapper
ในข้อมูลโค้ดที่แสดงไว้ด้านบนเราตกแต่ง method print_test ในคลาส method_decorator จะพิมพ์ชื่อเมืองถ้าชื่อของเมืองไม่ได้เป็น SFO
ใน Python Method คือฟังก์ชันที่ พารามิเตอร์แรกจะอ้างอิงกับอ็อบเจ็กต์ปัจจุบัน เราสามารถสร้าง decorators วิธีการเช่นเดียวกับในการพิจารณาตัวเองในฟังก์ชัน wrapper
โค้ด: เลือกทั้งหมด
def method_decorator(method):
def inner(city_instance):
if city_instance.name == "SFO":
print "Its a cool place to live in."
else:
method(city_instance)
return inner
class City(object):
def __init__(self, name):
self.name = name
@method_decorator
def print_test(self):
print self.name
p1 = City("SFO")
p1.print_test()
Its a cool place to live in.
First Bug Love you
-
- Similar Topics
- ตอบกลับ
- แสดง
- โพสต์ล่าสุด
-
-
โพสต์ใหม่ การใช้ Decorators เพื่อปรับปรุงความสามารถให้กับฟังก์ชันใน Python
โดย anawatj » 22/07/2023 2:23 pm » ใน Python Knowledge - 0 ตอบกลับ
- 1011 แสดง
-
โพสต์ล่าสุด โดย anawatj
22/07/2023 2:23 pm
-
-
-
โพสต์ใหม่ แนะนำเวอร์ชัน Python ในปัจจุบัน Python 3.10 หรือ Python 3.11
โดย athirach.offcial » 29/03/2024 3:20 pm » ใน Python Knowledge - 0 ตอบกลับ
- 838 แสดง
-
โพสต์ล่าสุด โดย athirach.offcial
29/03/2024 3:20 pm
-
-
-
โพสต์ใหม่ การใช้งาน Python GUI (Tkinter) : การเรียกใช้ from ใน python 2 และ python 3
โดย Jom07 » 06/03/2018 3:17 pm » ใน Python Knowledge - 0 ตอบกลับ
- 4453 แสดง
-
โพสต์ล่าสุด โดย Jom07
06/03/2018 3:17 pm
-
-
-
โพสต์ใหม่ ทำความรู้จักกับภาษา python (23) : การใช้ from...import (ฟอร์ม...อิมพอร์ต) ในภาษา python (ไพทอน)
โดย dawthana » 12/01/2017 3:24 pm » ใน Python Knowledge - 9 ตอบกลับ
- 6626 แสดง
-
โพสต์ล่าสุด โดย jirawoot
21/06/2019 10:07 am
-
ผู้ใช้งานขณะนี้
สมาชิกกำลังดูบอร์ดนี้: ไม่มีสมาชิกใหม่ และบุคลทั่วไป 100