程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python & c++ facial classic

編輯:Python

C/C++ Object oriented knowledge in

Object oriented programming (Object-oriented programming,OOP) There are three characteristics —— encapsulation 、 Inherit 、 polymorphic .

encapsulation : Encapsulate objective things into abstract classes , And classes can keep their data and methods to trusted classes or objects , Hiding information from untrusted sources .
keyword :public, protected, private. Do not write default is private.
1.public member : Can be accessed by any entity .
2.protected member : Only subclasses and member functions of this class are allowed to access .
3.private member : Only member functions of this class are allowed 、 Friend class or friend function access .

Inherit : Base class ( Parent class )——> Derived class ( Subclass )

polymorphic : That is, multiple states ( form ). Simply speaking , We can define polymorphism as the ability of messages to be displayed in many forms . Polymorphism is based on encapsulation and inheritance .
C++ Polymorphism classification and implementation :
1. Overloading Polymorphism (Ad-hoc Polymorphism, Compile time ): function overloading 、 Operator overloading
2. Subtype polymorphism (Subtype Polymorphism, The run-time ): Virtual functions
3. Parameter polymorphism (Parametric Polymorphism, Compile time ): Class template 、 Function templates
4. Forced polymorphism (Coercion Polymorphism, Compile time / The run-time ): Basic type conversion 、 Custom type conversion

Python Relevant knowledge of generator in

When we create a list , Limited by memory , The capacity must be limited , And it is impossible to enumerate them all at once .Python A fatal disadvantage of the commonly used list generation formula is that definition is generation , It's a waste of space and efficiency .

If list elements can be calculated by some algorithm , Then we can constantly calculate the following elements during the cycle , So you don't have to create a complete list, To save a lot of space . stay Python in , This kind of mechanism of calculating while cycling , It's called a generator :generator.

To create a generator, The simplest way is to transform the list generator :

a = [x * x for x in range(10)]
print(a)
b = (x * x for x in range(10))
print(b)
print(list(b))
for i in b:
print(i)
-------- give the result as follows --------------
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
<generator object <genexpr> at 0x10557da50>
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
0
1
4
9
16
25
36
49
64
81

Another method is the generator function , adopt def Definition , And then use yield To support iterator protocols , Easier to write than iterators .

def spam():
yield"first"
yield"second"
yield"third"
for x in spam():
print(x)
------- give the result as follows ---------
first
second
third

When you make a function call , Returns a generator object . In the use of next() When called , encounter yield Just go back to , Record the function call location at this time , Next call next() when , Start at the breakpoint .

We can use it just like using iterators generator , Except for the definition, of course . Define an iterator , Need to implement separately iter() Methods and next() Method , but generator Just a little yield.

generator also send() and close() Method , Only in next() After call , Only when the generator is in the suspended state .

python It supports the collaborative process , That is, microthreading , It is through generator To achieve . coordination generator We can customize the call hierarchy of functions to schedule threads by ourselves .

Python Knowledge about decorators in

Decorators allow you to pass existing functions to decorators , thus Add some additional functionality to existing functions , The decorator will perform the functions of the existing functions and the additional functions added .

The decorator is essentially a function , It allows existing functions to be added without any changes .

Next, we will use some examples to illustrate the function of the decorator :
If we don't use decorators , We usually insert logs before function execution in this way :

def foo():
print('i am foo')
def foo():
print('foo is running')
print('i am foo')

Although this is written to meet the needs , But changed the original code , If there are other functions that also need to insert logs , You need to rewrite all the functions , In this way, the code cannot be reused .
We can rewrite it as follows :

import logging
def use_log(func):
logging.warning("%s is running" % func.__name__)
func()
def bar():
print('i am bar')
use_log(bar) # Passing a function as an argument 
------------- The operation results are as follows --------------
WARNING:root:bar is running
i am bar

among ,use_log Functions are decorators , It puts the function we really want to execute bar() Encapsulated inside , Returns a new function that encapsulates the added code , It looks like bar() It's decorated .

But it's not implicit enough , We can go through @ Grammar sugar comes and goes bar = use_log(bar) The role of .

import logging
def use_log(func):
def wrapper(*args, **kwargs):
logging.warning('%s is running' % func.__name__)
return func(*args, **kwargs)
return wrapper
@use_log
def bar():
print('I am bar')
@use_log
def haha():
print('I am haha')
bar()
haha()
------------ give the result as follows ------------
WARNING:root:bar is running
I am bar
WARNING:root:haha is running
I am haha

TCP/IP Related concepts of the four layer model

TCP/IP Four layer model :

1. application layer : Responsible for protocols between different applications , Such as file transfer protocol (FTP), Remote login protocol (Telnet), Email protocol (SMTP), Network file service agreement (NFS), Network management protocol (SNMP) etc. .

2. Transport layer : Responsible for reliable transmission TCP agreement 、 Efficient transmission UDP agreement .

3. The network layer : Responsible for addressing ( Accurately find the other party's equipment ) Of IP,ICMP,ARP,RARP Such agreement .

4. Data link layer : Be responsible for transmitting digital signals in physical channels ( Ethernet cable ) Accurate transmission in .

Four layer model logic :
The sender is from top to bottom , Add the data of each layer protocol to the header of the data from the upper layer ( Radicals ) Then send it to the lower level .

The receiving end is from bottom to top , Decrypt the data received from the lower layer and remove the radicals of the header before sending it to the upper layer .

After layers of encryption and decryption , The application layer finally gets the required data .

OSI Related concepts of the seven layer model


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved