Searching...
فارسی
EnglishEnglish
EspañolSpanish
简体中文Chinese
FrançaisFrench
DeutschGerman
日本語Japanese
PortuguêsPortuguese
ItalianoItalian
한국어Korean
РусскийRussian
NederlandsDutch
العربيةArabic
PolskiPolish
हिन्दीHindi
Tiếng ViệtVietnamese
SvenskaSwedish
ΕλληνικάGreek
TürkçeTurkish
ไทยThai
ČeštinaCzech
RomânăRomanian
MagyarHungarian
УкраїнськаUkrainian
Bahasa IndonesiaIndonesian
DanskDanish
SuomiFinnish
БългарскиBulgarian
עבריתHebrew
NorskNorwegian
HrvatskiCroatian
CatalàCatalan
SlovenčinaSlovak
LietuviųLithuanian
SlovenščinaSlovenian
СрпскиSerbian
EestiEstonian
LatviešuLatvian
فارسیPersian
മലയാളംMalayalam
தமிழ்Tamil
اردوUrdu
Beyond the Basic Stuff with Python

Beyond the Basic Stuff with Python

Best Practices for Writing Clean Code
توسط Al Sweigart 2020 384 صفحات
4.27
100+ امتیازها
گوش دادن
Try Full Access for 7 Days
Unlock listening & more!
Continue

نکات کلیدی

1. پذیرش پیام‌های خطا و تسلط بر خط فرمان

به‌عنوان یک برنامه‌نویس، توانایی یافتن پاسخ‌ها به‌تنهایی بسیار مهم‌تر از هر گونه دانش الگوریتم یا ساختار داده است.

پیام‌های خطا دوستان شما هستند. به‌جای نادیده‌گرفتن پیام‌های خطا، یاد بگیرید که آن‌ها را تفسیر کنید. ردیابی‌ها مکان خطا را مشخص می‌کنند و خود پیام خطا سرنخ‌هایی درباره‌ی علت آن ارائه می‌دهد. پیام‌های خطا را کپی کرده و در موتورهای جستجو قرار دهید تا توضیحات و راه‌حل‌ها را پیدا کنید.

تسلط بر خط فرمان. خط فرمان ابزاری قدرتمند برای راه‌اندازی محیط، اجرای برنامه‌ها و خودکارسازی وظایف است. دستورات ضروری مانند cd، dir (یا lscopy (یا cp) و python را یاد بگیرید تا به‌طور مؤثر در سیستم‌فایل حرکت کرده و کد را اجرا کنید. تکمیل خودکار و تاریخچه‌ی دستورات می‌تواند به‌طور قابل‌توجهی تلاش‌های تایپ را کاهش دهد.

متغیرهای محیطی و PATH. درک متغیرهای محیطی، به‌ویژه متغیر PATH، برای اجرای برنامه‌ها از خط فرمان بسیار مهم است. افزودن دایرکتوری‌ها به PATH به شما این امکان را می‌دهد که برنامه‌ها را بدون مشخص‌کردن مسیر کامل فایل آن‌ها اجرا کنید.

2. فرمت‌بندی کد با بلک: ثبات کلید است

حفظ ثبات و خوانایی در یک پروژه، به‌جای پایبندی به هر قانون فرمت‌بندی فردی، دلیل اصلی اجرای راهنماهای سبک است.

خوانایی بسیار مهم است. فرمت‌بندی کد تنها جنبه‌ای ظاهری نیست؛ بلکه تأثیر قابل‌توجهی بر خوانایی دارد که برای نگهداری و همکاری در کد ضروری است. فرمت‌بندی منسجم بار شناختی را کاهش می‌دهد و درک منطق کد را آسان‌تر می‌کند.

بلک: فرمت‌کننده بی‌رحم. بلک فرمت‌بندی کد را خودکار می‌کند و ثبات را در پروژه‌های شما تضمین می‌کند. این ابزار به یک راهنمای سبک خاص پایبند است و بحث‌ها درباره‌ی فاصله‌ها، طول خطوط و سایر ترجیحات فرمت‌بندی را از بین می‌برد.

فاصله‌های افقی و عمودی. فاصله‌گذاری مناسب درون خطوط و بین بلوک‌های کد خوانایی را افزایش می‌دهد. از فاصله‌ها در اطراف عملگرها، پس از ویرگول‌ها و قبل از نظرات استفاده کنید. از خطوط خالی برای گروه‌بندی کدهای مرتبط و جداسازی بخش‌های مختلف استفاده کنید.

3. نام‌ها مهم هستند: شناسایی‌ها را با دقت انتخاب کنید

دو مشکل سخت در علوم کامپیوتر نام‌گذاری چیزها، نادرست‌سازی کش و خطاهای یک‌واحدی هستند.

نام‌های توصیفی حیاتی هستند. انتخاب نام‌های معنادار برای متغیرها، توابع و کلاس‌ها برای وضوح کد ضروری است. نام‌ها باید مختصر اما توصیفی باشند و از اختصارات، شوخی‌ها و ارجاعات فرهنگی پرهیز کنند.

قوانین نام‌گذاری. از قوانین نام‌گذاری PEP 8 برای کد پایتون پیروی کنید. از snake_case برای متغیرها و توابع، PascalCase برای کلاس‌ها و UPPER_SNAKE_CASE برای ثابت‌ها استفاده کنید.

طول مناسب. نام‌ها باید به اندازه کافی طولانی باشند تا توصیفی باشند اما نه آن‌قدر طولانی که غیرقابل‌مدیریت شوند. به دامنه نام توجه کنید: متغیرهای محلی می‌توانند نام‌های کوتاه‌تری داشته باشند، در حالی که متغیرهای جهانی به نام‌های توصیفی‌تری نیاز دارند.

4. شناسایی بوی کد برای جلوگیری از باگ‌ها

بوی کد لزوماً به این معنا نیست که مشکلی وجود دارد، اما به این معناست که باید برنامه‌تان را بررسی کنید.

کد تکراری. رایج‌ترین بوی کد، کد تکراری است که نگهداری را دشوار می‌کند. کد تکراری را با قرار دادن آن در توابع یا حلقه‌ها حذف کنید.

اعداد جادویی. مقادیر عددی غیرقابل‌توضیح را با ثابت‌های نام‌گذاری شده جایگزین کنید تا خوانایی و نگهداری بهبود یابد. این موضوع همچنین شامل رشته‌های متنی می‌شود.

بوی‌های دیگر کد. از کدهای کامنت‌شده، عبارات دیباگ چاپی، متغیرهایی با پسوند عددی و کلاس‌هایی که باید فقط توابع یا ماژول‌ها باشند، احتیاط کنید. این‌ها می‌توانند نشان‌دهنده‌ی مشکلات بالقوه یا زمینه‌های بهبود باشند.

5. نوشتن کد پایتونیک: پذیرش اصطلاحات زبان

باید یک راه واضح—و ترجیحاً تنها یک راه—برای انجام آن وجود داشته باشد.

سبک پایتون را بپذیرید. کد پایتونیک از ویژگی‌ها و اصطلاحات منحصر به فرد زبان برای ایجاد راه‌حل‌های مختصر و خوانا استفاده می‌کند. یادگیری این اصطلاحات برای نوشتن کد مؤثر پایتون ضروری است.

نحو معمولاً سوءاستفاده‌شده. از استفاده از range(len()) برای تکرار بر روی توالی‌ها خودداری کنید؛ به‌جای آن از enumerate() استفاده کنید. از عبارت with برای مدیریت فایل استفاده کنید تا از مدیریت صحیح منابع اطمینان حاصل کنید. مقایسه با None را با استفاده از is به‌جای == انجام دهید.

فرمت‌بندی رشته. از f-strings برای درج رشته‌ها استفاده کنید، زیرا آن‌ها جایگزینی خوانا و کارآمدتر نسبت به روش‌های قدیمی‌تر ارائه می‌دهند. رشته‌های خام برای رشته‌هایی با تعداد زیادی بک‌اسلش مفید هستند.

6. اندازه‌گیری عملکرد برای بهینه‌سازی مؤثر

بهینه‌سازی زودهنگام ریشه‌ی تمام شرارت‌هاست.

حدس نزنید، اندازه‌گیری کنید. قبل از تلاش برای بهینه‌سازی کد، عملکرد آن را با استفاده از ماژول‌های timeit و cProfile اندازه‌گیری کنید. این کار به شناسایی گلوگاه‌ها کمک می‌کند و اطمینان می‌دهد که تلاش‌های شما بر روی تأثیرگذارترین زمینه‌ها متمرکز است.

timeit برای قطعات کوچک. ماژول timeit زمان اجرای قطعات کوچک کد را اندازه‌گیری می‌کند و به شما این امکان را می‌دهد که عملکرد رویکردهای مختلف را مقایسه کنید.

cProfile برای توابع بزرگ‌تر. ماژول cProfile پروفایل دقیقی از فراخوانی توابع ارائه می‌دهد و نشان می‌دهد کدام توابع بیشترین زمان را مصرف می‌کنند. قانون آمدال بیان می‌کند که بهبود کندترین بخش‌های کد شما بیشترین افزایش عملکرد کلی را به همراه دارد.

7. برنامه‌نویسی شی‌گرا: کلاس‌ها و اشیاء

برنامه‌ها باید به‌گونه‌ای نوشته شوند که انسان‌ها بخوانند و تنها به‌طور تصادفی برای ماشین‌ها اجرا شوند.

کلاس‌ها به‌عنوان الگو. کلاس‌ها الگوهایی برای ایجاد اشیاء هستند که نمونه‌هایی از آن کلاس‌ها هستند. کلاس‌ها داده‌ها (ویژگی‌ها) و رفتارها (متدها) را در یک واحد واحد تجمیع می‌کنند.

متدها و __init__(). متدها توابعی هستند که به اشیاء مرتبط هستند. متد __init__() یک متد خاص است که ویژگی‌های شیء را هنگام ایجاد آن مقداردهی می‌کند. پارامتر self به خود شیء اشاره دارد.

ویژگی‌ها. ویژگی‌ها متغیرهایی هستند که به یک شیء مرتبط هستند. آن‌ها می‌توانند با استفاده از نوتیشن نقطه‌ای (به‌عنوان مثال، object.attribute) دسترسی و تغییر یابند.

8. وراثت: استفاده مجدد از کد با احتیاط

قرار دادن اشیاء در یک سلسله‌مراتب منظم به حس نظم ما appeals می‌کند؛ برنامه‌نویسان این کار را فقط برای سرگرمی انجام می‌دهند.

وراثت برای استفاده مجدد از کد. وراثت به شما این امکان را می‌دهد که کلاس‌های جدید (کلاس‌های فرزند) ایجاد کنید که متدها و ویژگی‌های کلاس‌های موجود (کلاس‌های والد) را به ارث می‌برند. این کار استفاده مجدد از کد را ترویج می‌دهد و تکرار را کاهش می‌دهد.

بازنویسی متدها. کلاس‌های فرزند می‌توانند متدهای به ارث برده شده را برای ارائه رفتار خاص بازنویسی کنند. تابع super() به شما این امکان را می‌دهد که متد کلاس والد را از متد کلاس فرزند فراخوانی کنید.

ترجیح ترکیب. ترکیب، که شامل گنجاندن اشیاء از کلاس‌های دیگر به‌عنوان ویژگی‌ها است، اغلب جایگزینی انعطاف‌پذیرتر برای وراثت است. این کار از اتصال محکم بین کلاس‌ها جلوگیری می‌کند و مدولار بودن را ترویج می‌دهد.

9. OOP پایتونیک: ویژگی‌ها و متدهای داندری

لطفاً کامپیوترها را انسان‌انگاری نکنید؛ آن‌ها این موضوع را بسیار آزاردهنده می‌یابند.

ویژگی‌ها برای دسترسی کنترل‌شده به ویژگی‌ها. ویژگی‌ها راهی برای کنترل نحوه‌ی دسترسی، تغییر و حذف ویژگی‌ها فراهم می‌کنند. آن‌ها به شما این امکان را می‌دهند که منطق اعتبارسنجی را اضافه کنید و از وضعیت‌های نامعتبر جلوگیری کنید.

متدهای داندری برای بارگذاری عملگر. متدهای داندری (که به آن‌ها متدهای جادویی نیز گفته می‌شود) به شما این امکان را می‌دهند که تعریف کنید اشیاء شما چگونه با عملگرهای داخلی پایتون (به‌عنوان مثال، +، -، *، ==) تعامل داشته باشند. این کار به شما این امکان را می‌دهد که کدهای بیانگر و شهودی‌تری ایجاد کنید.

OOP پایتونیک. با استفاده از ویژگی‌ها و متدهای داندری، می‌توانید کلاس‌هایی ایجاد کنید که بیشتر پایتونیک، خوانا و قابل‌نگهداری باشند. این ویژگی‌ها به شما این امکان را می‌دهند که از نقاط قوت منحصر به فرد پایتون بهره‌برداری کنید و کدی بنویسید که طبیعی و بیانگر باشد.

آخرین به‌روزرسانی::

FAQ

1. What is Beyond the Basic Stuff with Python by Al Sweigart about?

  • Bridges beginner to intermediate: The book is designed for readers who have completed basic Python tutorials and want to deepen their skills with best practices for writing clean, maintainable, and professional Python code.
  • Covers advanced topics: It explores environment setup, code formatting, naming conventions, debugging, Pythonic idioms, object-oriented programming, performance measurement, and practical projects.
  • Focus on real-world skills: The book explains programming jargon, common Python pitfalls, and introduces tools and techniques used by professional developers.
  • Guides through the “desert of despair”: Al Sweigart aims to help those who feel stuck between beginner and advanced resources, providing a clear path to becoming a more capable programmer.

2. Why should I read Beyond the Basic Stuff with Python by Al Sweigart?

  • Advance beyond basics: The book is ideal for those who want to move past beginner-level Python and learn industry-standard practices for writing robust, readable code.
  • Gain professional confidence: It helps readers overcome impostor syndrome by teaching tools, techniques, and mindsets used by experienced developers.
  • Learn essential development tools: Readers are introduced to Git for version control, project organization, and performance analysis, which are crucial for real-world programming.
  • Write more predictable code: The book emphasizes returning consistent data types, using exceptions for error handling, and adopting clear documentation practices.

3. Who is the ideal reader for Beyond the Basic Stuff with Python by Al Sweigart?

  • Intermediate Python learners: Those who have finished introductory books or tutorials and want to deepen their understanding of Python.
  • Programmers from other languages: Developers familiar with other programming languages who want to learn Python’s best practices without rehashing basic syntax.
  • Aspiring professionals: Anyone aiming to write code that is maintainable, readable, and suitable for collaborative or production environments.
  • Self-taught coders: Readers who want to fill in gaps in their knowledge and learn the “why” behind Python conventions.

4. What are the key takeaways from Beyond the Basic Stuff with Python by Al Sweigart?

  • Write clean, readable code: Emphasizes code formatting, naming conventions, and avoiding common code smells to improve clarity and maintainability.
  • Adopt Pythonic idioms: Encourages using features like enumerate(), f-strings, and context managers to write idiomatic Python.
  • Handle errors professionally: Teaches how to read tracebacks, use exceptions instead of error codes, and ask for help effectively online.
  • Understand performance and organization: Introduces performance measurement tools, Big O analysis, and project organization with Git and Cookiecutter.

5. What are the best practices for writing clean Python code according to Al Sweigart?

  • Consistent code formatting: Recommends using Black, an automatic code formatter, to enforce PEP 8 style and reduce debates over formatting.
  • Descriptive naming conventions: Advocates for clear, concise, and consistent names, avoiding jokes, puns, or overwriting built-in names.
  • Refactor code smells: Identifies issues like duplicate code, magic numbers, and poor error handling, encouraging regular refactoring for better design.
  • Comment and document wisely: Stresses writing comments that explain intent, using docstrings for functions and modules, and maintaining a professional tone.

6. How does Beyond the Basic Stuff with Python by Al Sweigart teach error handling and asking for help?

  • Read and interpret errors: Guides readers in understanding Python tracebacks and deciphering cryptic error messages to locate bugs efficiently.
  • Effective internet searches: Suggests copying error messages into search engines with quotes to find solutions, normalizing this as a key programming skill.
  • Best practices for asking questions: Details how to ask for help online by providing full error messages, complete code, setup details, and previous troubleshooting steps.
  • Use exceptions, not error codes: Recommends raising exceptions instead of returning error codes or None, making error handling clearer and less bug-prone.

7. What does Al Sweigart mean by “writing Pythonic code” in Beyond the Basic Stuff with Python?

  • Follow the Zen of Python: Encourages using Tim Peters’s aphorisms as guiding principles, such as “Beautiful is better than ugly” and “Explicit is better than implicit.”
  • Use idiomatic constructs: Promotes using features like enumerate(), with statements, and f-strings for more readable and efficient code.
  • Leverage dictionary methods: Explains using get(), setdefault(), and collections.defaultdict for concise and safe dictionary operations.
  • Prefer concise expressions: Advocates for Python’s ternary operator and other idioms to write clear, succinct code.

8. How does Beyond the Basic Stuff with Python by Al Sweigart explain Python programming jargon and concepts?

  • Clear definitions: Provides accessible explanations of terms like objects, values, instances, literals, keywords, and containers.
  • Distinguishes similar terms: Clarifies differences between statements vs. expressions, functions vs. methods, and iterable vs. iterator.
  • Explains Python internals: Covers topics like garbage collection, bytecode vs. machine code, and the roles of modules and packages.
  • Demystifies libraries and frameworks: Defines and contrasts libraries, frameworks, SDKs, and APIs for better conceptual understanding.

9. What are the most common Python pitfalls and “gotchas” highlighted by Al Sweigart?

  • Modifying lists during iteration: Warns against changing lists while looping, which can cause skipped items or infinite loops, and suggests safer alternatives.
  • Mutable default arguments: Explains why using mutable objects as default function arguments leads to unexpected behavior, recommending None with internal initialization.
  • Floating-point and sorting quirks: Discusses floating-point rounding errors and how Python’s sort() method sorts by ASCII values, advising use of key=str.lower for case-insensitive sorting.
  • Esoteric behaviors: Covers oddities like preallocated integers, string interning, and the absence of increment/decrement operators.

10. How does Beyond the Basic Stuff with Python by Al Sweigart recommend writing effective functions?

  • Descriptive, action-oriented names: Functions should have clear names, often including verbs, to indicate their purpose.
  • Keep functions concise: Recommends keeping functions under 30 lines when possible, but warns against over-fragmenting code.
  • Limit parameters and use defaults: Advises minimizing the number of parameters and using default arguments (carefully avoiding mutable defaults) to simplify function calls.
  • Advanced features: Explains *args and **kwargs for flexible argument handling, and introduces functional programming concepts like pure functions, higher-order functions, and lambda expressions.

11. What does Al Sweigart advise about comments, docstrings, and type hints in Beyond the Basic Stuff with Python?

  • Intent-focused comments: Comments should explain why code is written a certain way, not just what it does, to aid future maintainers.
  • Use docstrings for documentation: Docstrings provide structured, accessible documentation for functions and modules, viewable via Python’s help system.
  • Consistent comment style: Recommends single-line comments with a space after #, block comments for multi-line explanations, and sparing use of inline comments.
  • Type hints for clarity: Encourages gradual adoption of type hints to specify expected data types, improving code safety and enabling static analysis tools like Mypy.

12. How does Beyond the Basic Stuff with Python by Al Sweigart cover object-oriented programming (OOP) and advanced Python features?

  • Classes and methods: Explains how classes group data (attributes) and behavior (methods), with self and __init__() for object initialization.
  • Properties and dunder methods: Introduces properties for controlled attribute access and dunder methods like __add__(), __str__(), and __eq__() for operator overloading.
  • Favor composition over inheritance: Advises using composition (“has a” relationships) rather than inheritance (“is a” relationships) for more flexible and maintainable code.
  • Functional programming tools: Covers first-class functions, lambda expressions, higher-order functions, and variadic functions to write more expressive and efficient Python code.

نقد و بررسی

4.27 از 5
میانگین از 100+ امتیازات از Goodreads و Amazon.

کتاب فراتر از مباحث پایه با پایتون به خاطر پر کردن شکاف بین برنامه‌نویسی مبتدی و متوسط پایتون به شدت مورد تحسین قرار گرفته است. خوانندگان از پوشش بهترین شیوه‌ها، ابزارها و تکنیک‌ها، از جمله فرمت‌بندی کد، کنوانسیون‌های نام‌گذاری، استفاده از گیت و برنامه‌نویسی شیءگرا قدردانی می‌کنند. این کتاب به خاطر توضیحات واضح، مثال‌های عملی و تمرکز بر کد پایتونیک ستایش شده است. بسیاری از منتقدان آن را در پیشرفت مهارت‌ها و درک مفاهیم توسعه نرم‌افزار مفید یافته‌اند. در حالی که برخی احساس کردند که برخی موضوعات می‌توانستند عمیق‌تر بررسی شوند، اما اجماع کلی این است که این کتاب منبعی عالی برای یادگیرندگان پایتون است که به دنبال پیشرفت فراتر از مباحث پایه هستند.

Your rating:
4.6
15 امتیازها

درباره نویسنده

آل سوئیگارت یک توسعه‌دهنده نرم‌افزار و نویسنده کتاب‌های فناوری است که به خاطر رویکرد قابل دسترس و عملی‌اش در آموزش برنامه‌نویسی شناخته می‌شود. مشهورترین اثر او، "خودکار کردن کارهای خسته‌کننده با پایتون"، به بسیاری از مبتدیان کمک کرده است تا زبان پایتون را یاد بگیرند. آل سوئیگارت چندین کتاب دیگر در زمینه پایتون نوشته است که شامل عناوینی در زمینه توسعه بازی و برنامه‌نویسی برای کودکان می‌باشد. او به خاطر توانایی‌اش در توضیح مفاهیم پیچیده به زبان ساده و ایجاد تجربیات یادگیری جذاب و مبتنی بر پروژه شناخته شده است. کتاب‌های سوئیگارت معمولاً بر روی کاربردهای دنیای واقعی برنامه‌نویسی تمرکز دارند و به خوانندگان کمک می‌کنند تا مزایای عملی کدنویسی را ببینند. سبک نوشتاری او به خاطر وضوح، اختصار و مناسب بودن برای مبتدیان مورد تحسین قرار گرفته و برنامه‌نویسی را برای طیف وسیعی از مخاطبان قابل دسترس کرده است.

Listen to Summary
0:00
-0:00
1x
Dan
Andrew
Michelle
Lauren
Select Speed
1.0×
+
200 words per minute
Home
Library
Get App
Create a free account to unlock:
Requests: Request new book summaries
Bookmarks: Save your favorite books
History: Revisit books later
Recommendations: Personalized for you
Ratings: Rate books & see your ratings
100,000+ readers
Try Full Access for 7 Days
Listen, bookmark, and more
Compare Features Free Pro
📖 Read Summaries
All summaries are free to read in 40 languages
🎧 Listen to Summaries
Listen to unlimited summaries in 40 languages
❤️ Unlimited Bookmarks
Free users are limited to 10
📜 Unlimited History
Free users are limited to 10
Risk-Free Timeline
Today: Get Instant Access
Listen to full summaries of 73,530 books. That's 12,000+ hours of audio!
Day 4: Trial Reminder
We'll send you a notification that your trial is ending soon.
Day 7: Your subscription begins
You'll be charged on May 16,
cancel anytime before.
Consume 2.8x More Books
2.8x more books Listening Reading
Our users love us
100,000+ readers
"...I can 10x the number of books I can read..."
"...exceptionally accurate, engaging, and beautifully presented..."
"...better than any amazon review when I'm making a book-buying decision..."
Save 62%
Yearly
$119.88 $44.99/year
$3.75/mo
Monthly
$9.99/mo
Try Free & Unlock
7 days free, then $44.99/year. Cancel anytime.
Scanner
Find a barcode to scan

Settings
General
Widget
Loading...