1771 26

[其他] Python3.9有什么新的变化 [推广有奖]

11
数据洞见 发表于 2021-12-18 10:44:41
Conventional coding style
This primarily focuses on how we write function names, variable names, and module
names. Python has a coding system and naming conventions, which were discussed
in the previous chapter of this book. It is important to follow the coding and naming
conventions, especially when building reusable modules and packages. Otherwise,
we will be discussing such modules as bad examples of reusable modules.

12
数据洞见 发表于 2021-12-18 10:44:57
Building packages
There are a number of techniques and tools available for creating and distributing
packages. The truth is that Python does not have a great history of standardizing the
packaging process. There have been multiple projects started in the first decade of the 21st
century to streamline this process but not with a lot of success. In the last decade, we have
had some success, thanks to the initiatives of the Python Packaging Authority (PyPA).
In this section, we will be covering techniques of building packages, accessing the
packages in our program, and publishing and sharing the packages as per the guidelines
provided by PyPA.
We will start with package names, followed by the use of an initialization file, and then
jump into building a sample package.
Naming
Package names should follow the same rule for naming as for modules, which is lowercase
with no underscores. Packages act like structured modules.
Package initialization file
A package can have an optional source file named __init__.py (or simply an init
file). The presence of the init file (even a blank one) is recommended to mark folders as
packages. Since Python release 3.3 or later, the use of an init file is optional (PEP 420:
Implicit Namespace Packages). There can be multiple purposes of using this init file and
there is always a debate about what can go inside an init file versus what cannot go in.
A few uses of the init file are discussed here:
• Empty __init__.py: This will force developers to use explicit imports and manage
the namespaces as they like. As expected, developers have to import separate
modules, which can be tedious for a large package.
• Full import in __init__.py: In this case, developers can import the package and
then refer to the modules directly in their code using the package name or its alias
name. This provides more convenience but at the expense of maintaining the list of
all imports in the __init__ file.

13
数据洞见 发表于 2021-12-18 10:45:14
Building a package
Now we will discuss how to build a package with one sample package example. We will
build a masifutil package using the following modules and a sub-package:
• The mycalculator.py module: We already built this module for the Importing
modules section.
• The myrandom.py module: This module was also built for the Importing
modules section.
• The advcalc sub-package: This will be a sub-package and will contain one module
in it (advcalculator.py). We will define an init file for this sub-package but
it will be empty.

14
数据洞见 发表于 2021-12-18 10:45:16
Building a package
Now we will discuss how to build a package with one sample package example. We will
build a masifutil package using the following modules and a sub-package:
• The mycalculator.py module: We already built this module for the Importing
modules section.
• The myrandom.py module: This module was also built for the Importing
modules section.
• The advcalc sub-package: This will be a sub-package and will contain one module
in it (advcalculator.py). We will define an init file for this sub-package but
it will be empty.

15
数据洞见 发表于 2021-12-18 10:45:33
Accessing packages from any location
The package we built in the previous subsection is accessible only if the program calling
the modules is at the same level as the package location. This requirement is not practical
for code reusability and code sharing.
In this section, we will discuss a few techniques to make packages available and usable
from any program on any location in our system.
Appending sys.path
This is a useful option for setting sys.path dynamically. Note that sys.path is a list
of directories on which a Python interpreter searches every time it executes an import
statement in a source program. By using this approach, we are appending (adding) paths
of directories or folders containing our packages to sys.path.

16
玩于股涨之上 发表于 2021-12-18 10:52:45
We can use a special syntax to extract multiple characters from a string. The process
is called slicing. Place two numbers inside the square brackets, separated by a colon. The
left-side value sets the starting index. The right-side value sets the final index. The starting index is inclusive; Python includes the character at that index. The ending index is
exclusive; Python excludes the character at that index. Tricky, I know

17
玩于股涨之上 发表于 2021-12-18 10:53:41
  Simple data types
Data comes in a variety of types. A whole number like 5 is of a different type than a
decimal number like 8.46. Both 5 and 8.46 are different from a text value like "Bob".
Let’s begin with an exploration of the core data types built into Python. Make sure
that you’ve installed the Anaconda distribution and set up a conda environment that
includes the Jupyter Notebook coding environment. If you need help, see the installation instructions in appendix A. Activate the conda environment you created for this
book, execute the command jupyter notebook, and create a new Notebook.

18
玩于股涨之上 发表于 2021-12-18 10:53:57
  As a data analyst turned software engineer, I’ve witnessed the Python proficiency
requirements for many roles in the industry. I can state from experience that you do
not need to be an advanced programmer to be productive with pandas. Basic understanding of Python’s core mechanics, however, will significantly accelerate the speed
at which you can pick up the library. This appendix highlights the key language essentials you need to know to be successful.

19
玩于股涨之上 发表于 2021-12-18 10:54:14
A helpful way to think of objects is as digital building blocks. Consider a spreadsheet software like Excel. As users, we can discern the differences among a workbook, a worksheet, and a cell. A workbook holds worksheets, a worksheet holds
cells, and cells hold values. We view these three entities as three distinct containers
of business logic, each with a designated responsibility, and we interact with them
in different ways. When building object-oriented computer programs, developers
think in the same manner, identifying and building the “blocks” that need to exist
for a program to run.

20
玩于股涨之上 发表于 2021-12-18 10:54:30
Python is an object-oriented programming (OOP) language. The OOP paradigm views a software program as being a collection of objects that talk to one
another. An object is a digital data structure that stores information and provides
ways for it to be accessed and manipulated. Each object has a responsibility or purpose for existing. We can think of each object as being an actor in a play and the
software program as being a performance.

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-9 10:50