ทำความรู้จักกับภาษา python (44) : การจัดการกับ Object (ออบเจ็ค) ที่ไม่ได้ใช้
Moderators: mindphp, ผู้ดูแลกระดาน
-
- PHP Super Hero Member
- Posts: 604
- Joined: 07/12/2016 10:55 am
ทำความรู้จักกับภาษา python (44) : การจัดการกับ Object (ออบเจ็ค) ที่ไม่ได้ใช้
การจัดการกับ Object (ออบเจ็ค) ที่ไม่ได้ใช้
ใน python (ไพทอน) จะทำการลบตัวแปร หรือ Object ที่ไม่ได้ใช้ให้อัตโนมัติแล้วคืนค่าหน่วยความจำให้กับระบบ โดยใช้ฟังก์ชัน __del__() (เดล) เพื่อการบริหาร Memory (เมมโมรี่) ในการสร้าง Class (คลาส) ที่มีขนาดใหญ่ซึ่งเรียกการจัดการนี้ว่า “Destructor (ดีสทรัคเตอร์)”
ตัวอย่างการใช้ del statement (เดล สเตรทเม้นท์) ตัวอย่างการใช้ Destroy Object (ดิซทรอย ออบเจ็ค) ศึกษาข้อมูลมาจาก https://www.youtube.com/watch?v=RsFkuXB ... lzdKrpxsMM
ใน python (ไพทอน) จะทำการลบตัวแปร หรือ Object ที่ไม่ได้ใช้ให้อัตโนมัติแล้วคืนค่าหน่วยความจำให้กับระบบ โดยใช้ฟังก์ชัน __del__() (เดล) เพื่อการบริหาร Memory (เมมโมรี่) ในการสร้าง Class (คลาส) ที่มีขนาดใหญ่ซึ่งเรียกการจัดการนี้ว่า “Destructor (ดีสทรัคเตอร์)”
ตัวอย่างการใช้ del statement (เดล สเตรทเม้นท์) ตัวอย่างการใช้ Destroy Object (ดิซทรอย ออบเจ็ค) ศึกษาข้อมูลมาจาก https://www.youtube.com/watch?v=RsFkuXB ... lzdKrpxsMM
-
- PHP Super Hero Member
- Posts: 604
- Joined: 07/12/2016 10:55 am
-
- PHP Super Hero Member
- Posts: 813
- Joined: 08/01/2018 9:55 am
Re: ทำความรู้จักกับภาษา python (44) : การจัดการกับ Object (ออบเจ็ค) ที่ไม่ได้ใช้
Code: Select all
class test:
def __init__(self, a=0, b=0):
self.a = b
self.b = a
print("class")
def __del__(self):
class_name = self.__class__.__name__
print(class_name,"Function")
pt1 = test()
pt2 = pt1
pt3 = pt2
print(id(pt1))
print(id(pt2))
print(id(pt3))
I am slow walker, but I never walk back. (Abraham Lincoln)
- Jom07
- PHP Super Hero Member
- Posts: 514
- Joined: 08/01/2018 9:56 am
Re: ทำความรู้จักกับภาษา python (44) : การจัดการกับ Object (ออบเจ็ค) ที่ไม่ได้ใช้
Code: Select all
class ob:
def __init__(self, m=0, n=0):
self.n = n
self.m = m
print("Start class >>>>>")
def __del__(self):
class_name = self.__class__.__name__
print("The",class_name,"Function is Destroyed")
pt1 = ob()
pt2 = pt1
pt3 = pt2
print(id(pt1))
print(id(pt2))
print(id(pt3))
ศึกษาข้อมูลจาก :https://www.youtube.com/watch?v=RsFkuXB ... M&index=44

-
- PHP Hero Member
- Posts: 199
- Joined: 30/04/2018 9:44 am
Re: ทำความรู้จักกับภาษา python (44) : การจัดการกับ Object (ออบเจ็ค) ที่ไม่ได้ใช้
การจัดการกับ Object (ออบเจ็ค) ที่ไม่ได้ใช้
ผลการรัน
ศึกษาจาก : https://www.youtube.com/watch?v=RsFkuXB ... lzdKrpxsMM
Code: Select all
class Mindpoint:
def __init__(self, x=0, o=0):
self.x = o
self.o = x
print("Starting class :)")
def __del__(self):
class_name = self.__class__.__name__
print("This "+class_name+" Function is Deleting")
pt1 = Mindpoint()
pt2 = pt1
pt3 = pt2
print(id(pt1))
print(id(pt2))
print(id(pt3))
It’s never too late to start again.
-
- PHP Super Hero Member
- Posts: 561
- Joined: 02/07/2018 10:45 am
Re: ทำความรู้จักกับภาษา python (44) : การจัดการกับ Object (ออบเจ็ค) ที่ไม่ได้ใช้
เป็นคำสั่งที่ไว้ใช้ลบ คลาส ออกโดยจะคืนค่าสู่ เมมโมรี่
Code: Select all
#การลบค่าตัวแปร แบบ ทั่วๆไป
var_dic={"A":"A","B":"B","C":"C"}
print(var_dic)
del (var_dic["A"])
print(var_dic)
#การลบ class
class MindpPoint:
def __init__(self, x=0 ,y=0):
self.x = x
self.y = y
print("Start using class >>")
def __del__(self):
class_name = self.__class__.__name__
print("The",class_name,"function is Destroyed!!")
pt1 = MindpPoint() #นำความสามรถของ classมาเก็บไว้ในตัวแปร
pt2 = pt1
pt3=pt2
print(id(pt1)) #ที่อยู่บนเมมโมรี่ของ pt1
print(id(pt2)) #ที่อยู่บนเมมโมรี่ของ pt2
print(id(pt3)) #ที่อยู่บนเมมโมรี่ของ pt3
-
- PHP VIP Members
- Posts: 2984
- Joined: 04/06/2020 10:05 am
Re: ทำความรู้จักกับภาษา python (44) : การจัดการกับ Object (ออบเจ็ค) ที่ไม่ได้ใช้
Code: Select all
class delete:
def __init__(self, b=0, k=0):
self.b = b
self.k = k
print("class")
def __del__(self):
class_name = self.__class__.__name__
print("the",class_name,"Function is Destroyed!!")
pt1 = delete()
pt2 = pt1
pt3 = pt2
print(id(pt1))
print(id(pt2))
print(id(pt3))
-
- Similar Topics
- Replies
- Views
- Last post
-
- 8 Replies
- 2422 Views
-
Last post by bolue
09/06/2020 10:22 am
-
-
ORM: Object Relational Mapping (โออาร์เอ็ม: อ็อบเจค รีเรชั่นนอล แม็ปปิ้ง) คืออะไร
by md040 » 26/11/2016 10:44 am » in SQL Knowledge - 1 Replies
- 1034 Views
-
Last post by mindphp
11/08/2020 8:52 pm
-
-
-
ทำความรู้จักกับภาษา python (23) : การใช้ from...import (ฟอร์ม...อิมพอร์ต) ในภาษา python (ไพทอน)
by dawthana » 12/01/2017 3:24 pm » in Python Knowledge - 9 Replies
- 2559 Views
-
Last post by jirawoot
21/06/2019 10:07 am
-
-
-
ทำความรู้จักกับภาษา python (22) : การสร้าง Module (โมดูล) ใน python (ไพทอน)
by dawthana » 12/01/2017 11:33 am » in Python Knowledge - 9 Replies
- 1907 Views
-
Last post by jirawoot
20/06/2019 6:44 pm
-
-
-
ทำความรู้จักกับภาษา python (9) : ฟังก์ชันการแปลงชนิดข้อมูลใน python (ไพทอน)
by dawthana » 29/12/2016 3:15 pm » in Python Knowledge - 7 Replies
- 5081 Views
-
Last post by chatee supasand
07/06/2019 4:09 pm
-
-
-
ทำความรู้จักกับภาษา python (4) : ตัวดำเนินการใน python (ไพทอน)
by dawthana » 28/12/2016 10:05 am » in Python Knowledge - 11 Replies
- 1419 Views
-
Last post by Sirayu
19/06/2019 11:56 am
-
Who is online
Users browsing this forum: No registered users and 14 guests