Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python
Python
  1. คลาสและแอตทริบิวต์คลาสใน Python ถูก Garbage Collected เมื่อไหร่

    แอตทริบิวต์คลาส (Class Attribute) จะคงอยู่จนกว่าการอ้างอิง (Reference) ตัวสุดท้ายจะหายไป ตัวแปร Global (Global Variable) ก็เช่นกัน ไม่มีการรับประกันว่าจะอยู่ตลอดการทำงานของโปรแกรม นอกจากนี้ คลาสที่นิยามในระดับโมดูล (Module Scope) ก็เป็นตัวแปร Global ด้วย ดังนั้นคลาส (และแอตทริบิวต์ของมัน) จึงมีอายุ

  2. วิธีสืบทอดหลายคลาสใน Python (Multiple Inheritance)

    ตามเอกสาร Python ฟังก์ชัน super() ช่วยให้สามารถขยายคลาสหลายคลาสในการสืบทอดได้ มันคืนค่า Proxy Object ที่มอบหมายการเรียกเมธอดให้คลาสผู้มา (Parent) หรือคลาสพี่น้อง (Sibling) ประโยชน์สำหรับการเข้าถึงเมธอดที่ถูก Override ในคลาสลูก ลำดับการค้นหาเหมือนกับ getattr() แต่ข้ามตัวคลาสเองไป กล่าวคือ การเรียก s

  3. วิธีประกาศตัวแปร Global ในคลาส Python

    ตัวแปร Global (Global Variable) คือตัวแปรที่มีขอบเขตทั่วทั้งโปรแกรม สามารถมองเห็นและเข้าถึงได้จากทุกส่วน ยกเว้นถูกบังโดยตัวแปรชื่อเดียวกันในขอบเขตย่อย ชุดของตัวแปร Global ทั้งหมดเรียกว่า Global Environment หรือ Global Scope ประกาศตัวแปรเป็น Global โดยใช้คีย์เวิร์ด global นำหน้าชื่อตัวแปร ตัวแปรทุกต

  4. วิธีสร้างคลาส Python จากออบเจ็กต์ JSON

    สามารถใช้ไลบรารี python-jsonschema-objects ซึ่งสร้างบน jsonschema ไลบรารีนี้ให้การผูกคลาสอัตโนมัติจาก JSON Schema เพื่อใช้ใน Python ตัวอย่าง JSON Schema schema = { title: Example Schema, type: object, properties: { firstName: {type: string}, lastName: {type: string},

  5. พื้นฐาน OOP ใน Python คืออะไร

    แนวคิด OOP ใน Python Python เป็นภาษาโปรแกรมมิ่งเชิงวัตถุ (Object-Oriented Programming) สามารถสร้างและใช้งานคลาสและออบเจ็กต์ได้อย่างง่ายดาย แนวคิดหลักของ OOP ใน Python มีดังนี้: วัตถุ (Object) คลาส (Class) เมธอด (Method) การสืบทอด (Inheritance) พอลิมอร์ฟิซึม (Polymorphism) กา

  6. วิธีแปลงสตริงเป็นออบเจ็กต์คลาสใน Python

    เมื่อได้รับสตริงเป็นอินพุตจากผู้ใช้ และต้องการแปลงเป็นออบเจ็กต์คลาส หากมีคลาสชื่อนั้นอยู่ในเนมสเปซปัจจุบัน สามารถทำได้ 2 วิธีหลัก วิธีที่ 1: ใช้ eval() class Foobar: pass print(eval(Foobar)) print(type(Foobar)) ผลลัพธ์ __main__.Foobar <type classobj> วิธีที่ 2: ใช้ getattr() กับ sys.modul

  7. วิธีการนับฟังก์ชันของคลาสใน Python

    โค้ดตัวอย่างต่อไปนี้แสดงวิธีการแสดงรายการฟังก์ชัน (เมธอด) ของคลาสที่กำหนดโดยใช้โมดูล inspect ตัวอย่าง class foo: def __init__(self): self.x = x def bar(self): pass def baz(self): pass print(type(foo)) import inspect print(inspect.getmembers(foo, predicate=inspect

  8. อธิบาย Inheritance vs Instantiation สำหรับ Python Classes

    Inheritance (การสืบทอด) Python เป็น Object-Oriented Language รองรับ Inheritance รวมถึง Multiple Inheritance Classes สามารถสืบทอดจาก Classes อื่นๆ ได้ Class สามารถสืบทอด Attributes และ Behavior Methods จากอีก Class หนึ่ง เรียกว่า Superclass Class ที่สืบทอดจาก Superclass เรียกว่า Subclass หรือ Heir Cl

  9. Static Methods ใน Python Class คืออะไร?

    Python Class ใดๆ มี Method สามประเภท ได้แก่ Instance Methods, Class Methods และ Static Methods ตัวอย่าง พิจารณาโค้ด class OurClass: def method(self): return instance method called, self @classmethod def classmethod(cls): return class method called, cls @staticmet

  10. วิธี Instantiate Python Classes แบบ Dynamic

    เพื่อ Instantiate Python Class เราต้องได้ Class Name ก่อน ทำได้ด้วยโค้ดต่อไปนี้ def get_class(kls): parts = kls.split(.) module = ..join(parts[:-1]) m = __import__(module) for comp in parts[1:]: m = getattr(m, comp) return m m คือ Class เราสามารถ Instantiate Class นี้ไ

  11. Getters/Setters Methods สำหรับ Python Class คืออะไร?

    Getters และ Setters ใช้ในหลาย Object-Oriented Programming Languages เพื่อให้แน่ใจถึง Principle ของ Data Encapsulation พวกเขายังเรียกว่า Mutator Methods Data Encapsulation มองว่าเป็นการ Bundle Data กับ Methods ที่ Operate บน Data เหล่านั้น Methods เหล่านี้คือ Getter สำหรับ Retrieve Data และ Setter สำ

  12. วิธีดึง List of Methods ใน Python Class

    โค้ดต่อไปนี้พิมพ์ List of Methods ของ Class ที่กำหนด ดังนี้ ตัวอย่าง class foo: def __init__(self): self.x = x def bar(self): pass def baz(self): pass print(type(foo)) import inspect print(inspect.getmembers(foo, predicate=inspect.ismethod)) Output <type

  13. วิธีจัดระเบียบ Python Classes ใน Modules และ/หรือ Packages

    การจัดระเบียบ Python Classes เข้า Modules และ/หรือ Packages ขึ้นอยู่กับ Personal Preferences, Convenience, Readability และ Application เอง มักมีกรณีที่แต่ละ Class อยู่ในไฟล์แยกกัน อาจเป็นว่าบางครั้งการเก็บ Related Classes ในไฟล์เดียวทำให้ใหญ่เกินไป บางครั้งก็พอดี ก็ทั้งหมดขึ้นอยู่กับ Scenarios, Conv

  14. วิธี Split Python Class เป็น Multiple Files

    เพื่อ Split Python Program หรือ Class เป็น Multiple Files เราต้อง Refactor และ Rewrite Code เป็นสอง Class หรือมากกว่าตามสะดวก พร้อมกันรับประกันว่า Functionality ของ Original Code ยังคงรักษาไว้ คำตอบของคำถามนี้เป็นไปตามข้างต้นในลักษณะ Generic และต้องการ Specific Solutions ขึ้นอยู่กับ Specific Scenari

  15. ข้อดีของการใช้ Python Class

    Classes เป็น Mechanism จัดระเบียบ Code ให้เป็น Generic, Reusable Pieces of Code ในที่สุดพวกเขาคือ Reusable Code Snippets ที่จะถูกใช้ซ้ำๆ ด้วยการปรับเปลี่ยนน้อยหรือไม่มีเลย Class Concept ได้รับแรงบันดาลใจจาก Biological Collections ของ Features (Attributes) และ Abilities (Methods) Functions เหมาะสำหร

  16. ที่ไหนเป็นที่เหมาะสมที่สุดสำหรับ Python Classes ใน Django Project?

    คุณสามารถวาง Class ได้ที่ไหนก็ตามที่ต้องการ ตัวอย่าง project foo bar.py baz.py qux.py จากนั้น Import Module qux.py ได้ด้วย Import from project.foo.qux import * การสร้าง Directory ดีกว่าเมื่อจัดการกับไฟล์จำนวนมาก ตัวอย่าง - project foo bar.py baz.py extras qux.

  17. Declarations ใน Python Class เทียบเท่ากับใน __init__ Method หรือไม่?

    Declarations ใดๆ ใน Class (นอกจากใน __init__) และ Declarations ใน __init__ Method ไม่เหมือนกัน โค้ดต่อไปนี้แสดงให้เห็นว่าจริง ตัวอย่าง import sys class foo(): print within class def __init__(self): print within init def do_smthng(self): print do something def main()

  18. วิธี Attach C Method ให้ Existing Python Class

    วิธีนี้แสดงวิธี Define Python Class ใหม่จาก C Extension Module Methods ของ Class ถูก Implement ใน C แต Class ยังสามารถ Instantiate, Subclass และ Extend จาก Python ได้ เทคนิคเดียวกันกับ Inheritance ยังสามารถใช้ Extend Existing Python Class ด้วย Methods ที่เขียนใน C ได้ ในเทคนิคนี้ Argument แรกของ PyC

  19. วิธีตรวจสอบว่า Python Object เป็น String หรือไม่

    สำหรับ Python 2.x ตรวจสอบว่า Object obj เป็น String Type หรือ Subclass ของ String Type - isinstance(obj, basestring) เพราะทั้ง str และ unicode เป็น Subclasses ของ basestring ตรวจสอบว่า obj เป็น Instance ของ str หรือ Subclass ใดๆ ของ str - isinstance(obj, str) ตรวจสอบว่า obj เป็น Instance ของ U

  20. วิธี Look Inside Python Object

    Python มี Built-in Functions ชุดแข็งแกร่งต่อไปนี้เพื่อรวบรวมข้อมูลเกี่ยวกับ Attributes, Properties และ Methods ของ Python Object ใดๆ - type() dir() id() getattr() hasattr() globals() locals() callable() type() และ dir() มีประโยชน์โดยเฉพาะสำหรับการหา Type ของ Object และ Set ของ Attributes ตามลำดั

Total 9001 -คอมพิวเตอร์  FirstPage PreviousPage NextPage LastPage CurrentPage:48/451  20-คอมพิวเตอร์/Page Goto:1 42 43 44 45 46 47 48 49 50 51 52 53 54