Python Decorators

แชร์ความรู้ภาษา Python ไพทอน การเขียนโปรแกรมภาษาไพทอน

Moderator: mindphp, ผู้ดูแลกระดาน

ภาพประจำตัวสมาชิก
nuattawoot
PHP VIP Members
PHP VIP Members
โพสต์: 2561
ลงทะเบียนเมื่อ: 05/06/2017 9:34 am

Python Decorators

โพสต์ที่ยังไม่ได้อ่าน โดย nuattawoot »

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
แก้ไขล่าสุดโดย nuattawoot เมื่อ 26/09/2017 6:59 pm, แก้ไขไปแล้ว 4 ครั้ง.
First Bug Love you
รูปภาพ
ภาพประจำตัวสมาชิก
nuattawoot
PHP VIP Members
PHP VIP Members
โพสต์: 2561
ลงทะเบียนเมื่อ: 05/06/2017 9:34 am

Re: Python Decorators

โพสต์ที่ยังไม่ได้อ่าน โดย nuattawoot »

Python decorators in classes

โค้ด: เลือกทั้งหมด

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
เป็นตังครอบคลุมไวยากรณ์ decorator และแนวคิดของ decorator (หรือ decorating) callable
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 คือ:
ชื่อของฟังก์ชั่นจะปรากฏขึ้นเพียงครั้งเดียวในไฟล์ต้นฉบับเท่านั้น
ผู้อ่านรู้ก่อนที่คำนิยามที่ยาวพอสมควรของฟังก์ชันฟังก์ชันมัณฑนากรจะถูกนำไปใช้
ไวยากรณ์ของ 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...>>
เมื่อเราเรียกเมธอด bound methods a จะถูกส่งผ่านเป็นอาร์กิวเมนต์

โค้ด: เลือกทั้งหมด

 a.method('an arg')
(<A object at 0x...>, 'an arg')
 a.method('an arg')[0] is a
True
staticmethod()
วิธีการแบบสร้างเมธอดที่ถูกผูกไว้เมื่อเข้าถึงฟังก์ชัน

โค้ด: เลือกทั้งหมด

class A(object):
     @staticmethod
     def method(*argv):
         return argv
 a = A()
 a.method
<function method at 0x...>
เมื่อเราเรียกวิธีคงที่เราไม่ได้รับอาร์กิวเมนต์เพิ่มเติมใด ๆ

โค้ด: เลือกทั้งหมด

a.method('an arg')
('an arg',)
The call() decorator
สมมติว่าเราต้องการสร้างตารางค้นหาโดยกล่าวว่าประกอบด้วยเต็มบวกจำนวน 0 ถึง n
สำหรับ n เล็กเราสามารถรับเข้ามาได้:

โค้ด: เลือกทั้งหมด

table = [0, 1, 4, 9, 16]
len(table), table[3]
(5, 9)
สูตรนี้ ทำความเข้าใจง่าย เลยใช้สูตรนี้

โค้ด: เลือกทั้งหมด

table = [i * i for i in range(5)]
len(table), table[3]
(5, 9)
First Bug Love you
รูปภาพ
ภาพประจำตัวสมาชิก
nuattawoot
PHP VIP Members
PHP VIP Members
โพสต์: 2561
ลงทะเบียนเมื่อ: 05/06/2017 9:34 am

Re: Python Decorators

โพสต์ที่ยังไม่ได้อ่าน โดย nuattawoot »

Python decorators in Method
ใน 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.
ในข้อมูลโค้ดที่แสดงไว้ด้านบนเราตกแต่ง method print_test ในคลาส method_decorator จะพิมพ์ชื่อเมืองถ้าชื่อของเมืองไม่ได้เป็น SFO
First Bug Love you
รูปภาพ
ตอบกลับโพส
  • Similar Topics
    ตอบกลับ
    แสดง
    โพสต์ล่าสุด

ผู้ใช้งานขณะนี้

สมาชิกกำลังดูบอร์ดนี้: Google Adsense [Bot] และบุคลทั่วไป 95