Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Expert Python Programming – Fourth Edition
Expert Python Programming – Fourth Edition

Expert Python Programming – Fourth Edition: Master Python by learning the best coding practices and advanced programming concepts , Fourth Edition

Arrow left icon
Profile Icon Michał Jaworski Profile Icon Tarek Ziadé Profile Icon Ziadé
Arrow right icon
$19.99 per month
Full star icon Full star icon Full star icon Full star icon Half star icon 4.4 (24 Ratings)
Paperback May 2021 630 pages 4th Edition
eBook
$9.99 $39.99
Paperback
$48.99
Subscription
Free Trial
Renews at $19.99p/m
Arrow left icon
Profile Icon Michał Jaworski Profile Icon Tarek Ziadé Profile Icon Ziadé
Arrow right icon
$19.99 per month
Full star icon Full star icon Full star icon Full star icon Half star icon 4.4 (24 Ratings)
Paperback May 2021 630 pages 4th Edition
eBook
$9.99 $39.99
Paperback
$48.99
Subscription
Free Trial
Renews at $19.99p/m
eBook
$9.99 $39.99
Paperback
$48.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
Table of content icon View table of contents Preview book icon Preview Book

Expert Python Programming – Fourth Edition

Modern Python Development Environments

A deep understanding of the programming language of choice is the most important part of being a programming expert. Still, it is really hard to develop good software efficiently without knowing the best tools and practices that are common within the given language community. Python has no single feature that cannot be found in some other language. So, when comparing the syntax, expressiveness, or performance, there will always be a solution that is better in one or more fields. But the area in which Python really stands out from the crowd is the whole ecosystem built around the language. The Python community has spent many years polishing standard practices and libraries that help to create high-quality software in a shorter time.

Writing new software is always an expensive and time-consuming process. However, being able to reuse existing code instead of reinventing the wheel greatly reduces development times and costs. For some...

Technical requirements

You can install the free system virtualization tools that are mentioned in this chapter from the following sites:

The following are the Python packages that are mentioned in this chapter that you can download from PyPI:

  • poetry
  • flask
  • wait-for-it
  • watchdog
  • ipython
  • ipdb

Information on how to install packages is included in the Installing Python packages using pip section.

The code files for this chapter can be found at https://github.com/PacktPublishing/Expert-Python-Programming-Fourth-Edition/tree/main/Chapter%202.

Python's packaging ecosystem

The core of Python's packaging ecosystem is the Python Packaging Index. PyPI is a vast public repository of (mostly) free-to-use Python projects that at the time of writing hosts almost three and a half million distributions of more than 250,000 packages. That's not the biggest number among all package repositories (npm surpassed a million packages in 2019) but it still places Python among the leaders of packaging ecosystems.

Such a large ecosystem of packages doesn't come without a price. Modern applications are often built using multiple packages from PyPI that often have their own dependencies. Those dependencies can also have their own dependencies. In large applications, such dependency chains can go on and on. Add the fact that some packages may require specific versions of other packages and you may quickly run into dependency hell—a situation where it is almost impossible to resolve conflicting version requirements...

Isolating the runtime environment

When you use pip to install a new package from PyPI, it will be installed into one of the available site-packages directories. The exact location of site-packages directories is specific to the operating system. You can inspect paths where Python will be searching for modules and packages by using the site module as a command as follows:

$ python3 -m site

The following is an example output of running python3 -m site on macOS:

sys.path = [
    '/Users/swistakm',
    '/Library/Frameworks/Python.framework/Versions/3.9/lib/python39.zip',
    '/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9',
    '/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/lib-dynload',
    '/Users/swistakm/Library/Python/3.9/lib/python/site-packages',
    '/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages',
]
USER_BASE: '/Users/swistakm/Library...

Application-level environment isolation

Python has built-in support for creating virtual environments. It comes in the form of a venv module that can be invoked directly from your system shell. To create a new virtual environment, simply use the following command:

$ python3.9 -m venv <env-name>

Here, env-name should be replaced with the desired name for the new environment (it can also be an absolute path). Note how we used the python3.9 command instead of plain python3. That's because depending on the environment, python3 may be linked to different interpreter versions and it is always better to be very explicit about the Python version when creating new virtual environments. The python3.9 -m venv commands will create a new env-name directory in the current working directory path. Inside, it will contain a few sub-directories:

  • bin/: This is where the new Python executable and scripts/executables provided by other packages are...

System-level environment isolation

The key enabler to the rapid iteration of software implementation is the reuse of existing software components. Don't repeat yourself—this is a common mantra of many programmers. Using other packages and modules to include them in the codebase is only a part of that mindset. What can also be considered as reused components are binary libraries, databases, system services, third-party APIs, and so on. Even whole operating systems should be considered as a component that is being reused.

The backend services of web-based applications are a great example of how complex such applications can be. The simplest software stack usually consists of a few layers. Consider some imaginary application that allows you to store some information of its users and exposes it to the internet over the HTTP protocol. It could have at least the three following layers (starting from the lowest):

  • A database or other kind of...

Popular productivity tools

Almost every open-source Python package that has been released on PyPI is a kind of productivity booster—it provides ready-to-use solutions to some problem. That way we don't have to reinvent the wheel all the time. Some could also say that Python itself is all about productivity. Almost everything in this language and the community surrounding it seems to be designed to make software development as productive as possible.

This creates a positive feedback loop. Since writing code with Python is fun and easy, a lot of programmers use their free time to create tools that make it even easier and more fun. And this fact will be used here as a basis for a very subjective and non-scientific definition of a productivity tool—a piece of software that makes development easier and more fun.

By nature, productivity tools focus mainly on certain elements of the development process, such as testing, debugging, and managing packages, and are...

Summary

This chapter was all about development environments for Python programmers. We've discussed the importance of environment isolation for Python projects. You've learned two different levels of environment isolation (application-level and system-level), and multiple tools that allow you to create them in a consistent and repeatable manner. We've also discussed some essential topics for managing Python dependencies in your projects. This chapter ended with a review of a few tools that improve the ways in which you can experiment with Python or debug your programs and work effectively.

Once you have all of these tools in your tool belt, you are well-prepared for the next few chapters, where we will discuss multiple features of modern Python syntax. You're probably already hungry for Python code so we will start with a quick overview of the new things that were included in Python over the last few releases.

If you're quite up to date with what&apos...

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Discover the new features of Python, such as dictionary merge, the zoneinfo module, and structural pattern matching
  • Create manageable code to run in various environments with different sets of dependencies
  • Implement effective Python data structures and algorithms to write, test, and optimize code

Description

This new edition of Expert Python Programming provides you with a thorough understanding of the process of building and maintaining Python apps. Complete with best practices, useful tools, and standards implemented by professional Python developers, this fourth edition has been extensively updated. Throughout this book, you’ll get acquainted with the latest Python improvements, syntax elements, and interesting tools to boost your development efficiency. The initial few chapters will allow experienced programmers coming from different languages to transition to the Python ecosystem. You will explore common software design patterns and various programming methodologies, such as event-driven programming, concurrency, and metaprogramming. You will also go through complex code examples and try to solve meaningful problems by bridging Python with C and C++, writing extensions that benefit from the strengths of multiple languages. Finally, you will understand the complete lifetime of any application after it goes live, including packaging and testing automation. By the end of this book, you will have gained actionable Python programming insights that will help you effectively solve challenging problems.

Who is this book for?

The Python programming book is intended for expert programmers who want to learn Python’s advanced-level concepts and latest features. Anyone who has basic Python skills should be able to follow the content of the book, although it might require some additional effort from less experienced programmers. It should also be a good introduction to Python 3.9 for those who are still a bit behind and continue to use other older versions.

What you will learn

  • Explore modern ways of setting up repeatable and consistent Python development environments
  • Effectively package Python code for community and production use
  • Learn modern syntax elements of Python programming, such as f-strings, enums, and lambda functions
  • Demystify metaprogramming in Python with metaclasses
  • Write concurrent code in Python
  • Extend and integrate Python with code written in C and C++

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : May 28, 2021
Length: 630 pages
Edition : 4th
Language : English
ISBN-13 : 9781801071109
Category :
Languages :

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details

Publication date : May 28, 2021
Length: 630 pages
Edition : 4th
Language : English
ISBN-13 : 9781801071109
Category :
Languages :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total $ 145.97
Expert Python Programming – Fourth Edition
$48.99
Learn Python Programming, 3rd edition
$46.99
Python Object-Oriented Programming
$49.99
Total $ 145.97 Stars icon
Banner background image

Table of Contents

15 Chapters
Current Status of Python Chevron down icon Chevron up icon
Modern Python Development Environments Chevron down icon Chevron up icon
New Things in Python Chevron down icon Chevron up icon
Python in Comparison with Other Languages Chevron down icon Chevron up icon
Interfaces, Patterns, and Modularity Chevron down icon Chevron up icon
Concurrency Chevron down icon Chevron up icon
Event-Driven Programming Chevron down icon Chevron up icon
Elements of Metaprogramming Chevron down icon Chevron up icon
Bridging Python with C and C++ Chevron down icon Chevron up icon
Testing and Quality Automation Chevron down icon Chevron up icon
Packaging and Distributing Python Code Chevron down icon Chevron up icon
Observing Application Behavior and Performance Chevron down icon Chevron up icon
Code Optimization Chevron down icon Chevron up icon
Other Books You May Enjoy Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Full star icon Full star icon Half star icon 4.4
(24 Ratings)
5 star 79.2%
4 star 8.3%
3 star 0%
2 star 0%
1 star 12.5%
Filter icon Filter
Top Reviews

Filter reviews by




N Satpall Jun 25, 2021
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This book is a great resource for expert programmers who want to learn about Python's advanced-level concepts and features in its newest releases. It focuses so well on tools and practices that are crucial for creating performant, reliable, and maintainable software in Python.It not only shows how Python is constantly changing, but also why it is changing. It showcases recent Python language additions and describes modern ways of setting up repeatable and consistent development environments for Python programmers.The book can also be a good resource for hobbyists who are interested in learning advanced-level concepts with Python, as also for programmers with experience in other languages by explaining how to integrate code written in different languages in their Python application. There are many practical illustrations of design patterns, programming paradigms, and metaprogramming techniques.It covers tools that can be used to assess code quality metrics and improve code style in fully automated way, while showing how to scale simple observability practices to large-scale distributed systems.
Amazon Verified review Amazon
Pax Jul 29, 2021
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I have 7+ years of professional experience as a programmer and have been coding in Python since 2019. I love the comprehensiveness of this book and I learned so much not only "how to do X" but "why to do X" --- it gave best practices and I'm not just repeating what the book tagline says. For someone with limited experience working in big companies / teams, the book is very insightful.It is also easy to read; I finished this book in ~2 weeks by setting a goal of reading 35 pages/day. (Some days I read more)My only "complaint" is that there were a bit of typos but they weren't super critical. It was very obvious that they are typos and they're not a lot so it's not super distracting and most importantly, you won't be left "confused."
Amazon Verified review Amazon
R.Thompson Aug 25, 2021
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Written by experts in python programming, learn code optimization, memory profiling, resource allocation and much more.
Amazon Verified review Amazon
hawkinflight Jun 22, 2021
Full star icon Full star icon Full star icon Full star icon Full star icon 5
The media could not be loaded. I have about two years of experience using Python, and I find this book very helpful in moving forward. The first chapter gets everyone off to a great start by sharing ideas about how to stay up-to-date with Python, as the language inevitably changes, and where to find Python community. All of the chapters are important and contain great information. I particularly like Chapter 4 which compares Python to other languages, and emphasizes that just because you might be able to write code as you would in another language, that is not necessarily the "Python way", nor the best way to do it in Python. The chapter identifies places where programmers might try things they really shouldn't. I have combined C/C++ code with Python, and so, I enjoyed reviewing the chapter which covers this topic. I closely read and enjoyed the chapter on Optimizing Code, one way of course is via choosing the best data structure. This helps the programmer learn not just what data structures are available but provides info on how data structures can impact performance. I have not had to profile code, but I think Chapter 12 would be very useful on that. I am very interested in going further and carefully reading the chapters on Interfaces, Patterns, and Modularity, on Testing and QA, as well as Concurrency, and Meta-programming. There is a lot of great material here.
Amazon Verified review Amazon
Stephan Miller Jun 26, 2021
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I have written Python code for about 15 years now. Even though many times that hasn't been what I wrote for my day job, it is still my favorite programming language. I didn't think there was much to learn, but then again I started as a Python 2 developer and only switched to Python 3 in the last few years.But this book is great and taught me a bunch of new things. I learned about Poetry, which I never heard of until now, and can't wait to use it on my next project. It also walks you through using Docker for development and explains why you may want to still use Vagrant in some cases even though it is an older technology. It even goes into writing C extensions to give your Python projects a performance boost.You should know some Python before reading this book, as it goes into some concepts in-depth, but I would recommend it to anyone who has been writing Python code for a few months.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.