Basic knowledge of Python

Basic knowledge of Python (6)

Management of Packages and Modules

(1) Module: script file suffixed with. py

  Module models.py


View.py Module

(2) Directives

1. Import usage: (import all packages in the module)

Import all members in models
In the models.py file:
Import models module in main.py file
Operation results:

Import all members in views.py:

import views

print('hello')


def  hello():
     print(models.page)


if  __name__ == "__main__":
  views.test()

Operation results:

2. Use of from import

Designated members of imported files

from models import page

print('hello')


def  hello():
     print(page)


hello()

Operation results:

All members of the imported file:

from models import *

print('hello')


def  hello():
     print(page)


hello()

Operation results:

3. Avoid renaming and rename members of the same name in different documents

from models import  test  as  m_test
from views import test as v_test

v_test()

Operation results:

4. importlib.reload (module)

Running steps bring problems: every time the import operation is executed on python console, it compiles into bytecode first, then runs module files. After the original file changes, the console saves the files generated by the original bytecode, so the running results remain unchanged.

On the console:

Solution: Reload using importlib. reload (module)
Console program:

>>>import  importlib
>>>importlib.reload(models)
<module 'models' from 'E:\\mypython03\\models.py'>
>>>models.test()
//Excellent Classroom uke.cc
models.test()

Easy to make grammatical errors: When importing file members, you cannot use from import grammar.
Original. py file

Console import file:
Change. py file

(3) Reasons for writing separate script files

Implementing code reuse
Save namespace
Implementing data or service sharing

(4) Steps:

1. Finding Module Files
 2. Compiled to bytecode  
3. Running module file

(5) Scope of search

 1. Program Home Directory
 2. Environmental variables
 3. Standard Library
 4. Extension Library (Priority Written by Yourself)

(6) Bags

Right-click on the project, select new, and click python package.
Create files under the package:

Use the _init_.py file

On the console:

Note: The established package is valid only at the first use, and the violent code is executed only once.

2. Object-Oriented Programming

Features: encapsulation, inheritance, polymorphism
 Example:
def search_book(title):
      print('Search for Containment Book Keyword{}Books'.format(title))


book = {
    'title':'Python Introduction',
    'price':39.00,
    'author':'Peter',
    'search_book':search_book
}

print(book['title'])
print(book.get('price',0.0))
book.get('search_book')('Python')

Operation results:

(1) Analysis steps

   OOA Object-Oriented Analysis
   OOD Object-Oriented Design
   OOP Object-Oriented Programming

(2) Implementation steps

  1. Analysis of Object Characteristic Behavior
  2. Writing Class Description Object Template
  3. Instance and simulation
import  datetime
class Book:
    def  __init__(self,title,price,author,publisher,pubdate):
        self.title = title
        self.price = price
        self.author = author
        self.publisher = publisher
        self.pubdate = pubdate


book1 = Book('C#Excellent Classics', 29.9,'Tom','Excellent Classroom', datetime.date(2016,3,1))


print(book1.title)
print(book1.price)

Operation results:

Class Definition

import  datetime
class Book:
    def  __init__(self,title,price=0.0,author=' ',publisher=None,pubdate=datetime.date.today()):
        self.title = title
        self.price = price
        self.author = author
        self.publisher = publisher
        self.pubdate = pubdate

    def print_info(self):
        print('The current information in this book is as follows:')
        print('Title:{}'.format(self.title))
        print('Price:{}'.format(self.price))
        print('Authors:{}'.format(self.author))
        print('Press:{}'.format(self.publisher))
        print('Publication time:{}'.format(self.pubdate))


book1 = Book('C#Excellent Classics', 29.9,'Tom','Excellent Classroom', datetime.date(2016,3,1))
book1.print_info()


book2 = Book('flask  Beginning to Proficiency')
book2.print_info()

Operation results

(iii) repr(self): the representation of classes that decide to print definitions on the console

import  datetime
class Book:
    def  __init__(self,title,price=0.0,author=' ',publisher=None,pubdate=datetime.date.today()):
        self.title = title
        self.price = price
        self.author = author
        self.publisher = publisher
        self.pubdate = pubdate

    def __repr__(self):
        return '<books {}>'.format(self.title)

    def print_info(self):
        print('The current information in this book is as follows:')
        print('Title:{}'.format(self.title))
        print('Price:{}'.format(self.price))
        print('Authors:{}'.format(self.author))
        print('Press:{}'.format(self.publisher))
        print('Publication time:{}'.format(self.pubdate))


book1 = Book('C#Excellent Classics', 29.9,'Tom','Excellent Classroom', datetime.date(2016,3,1))
book1.print_info()


book2 = Book('flask  Beginning to Proficiency')
book2.print_info()

Tags: Python Programming

Posted on Sun, 08 Sep 2019 07:13:59 -0700 by iamngk