The reflection of Python learning

Source: Internet
Author: User

Python Learning reflex


Example 1

The script reads as follows:

#coding: UTF8


Class MyClass (object):

def fax (self):

print ' faxing .... '

def copy (self):

print ' Copying ... '


def printing ():

print ' Printing .... '


m = MyClass ()

If Hasattr (M, ' fax '): #判断myClass类中是否有fax方法

Func = GetAttr (M, ' fax ') #返回内存对象

Func () #调用

SetAttr (M, ' print2 ', printing)

M.print2 ()



Try

#delattr (M, ' copy ')

#print Name

#print DFDF

#print TTTT

Print m

Except Attributeerror,e:

print ' something wrong. ', E


Except Exception,e:

Print E


Finally

print '-----------'

#else:

# print "Print nothing wrong ...."

The execution script results are as follows:

Faxing ....

Printing ....

<__main__.myclass Object at 0x028211d0>

-----------


Example 2

The script reads as follows:

#/usr/bin/env python

#coding: UTF8


Import Sys


Class WebServer (object):

def __init__ (Self,host,port):

Self.host = Host

Self.port = Port


def start (self):

Print "Server is starting ..."


def stop (self):

Print "Server is stopping ..."


def restart (self):

Self.stop ()

Self.start ()


if __name__ = = "__main__":

Server = WebServer (' localhost ', 80)

#print Sys.argv[1]

Cmd_dic = {

' Start ': Server.start,

' Stop ': server.stop,

' Restart ': Server.restart,

}

#if sys.argv[1] = = ' Start ':

If sys.argv[1] in Cmd_dic:

CMD_DIC[SYS.ARGV[1]] ()

The execution script results are as follows:

D:\Python learn \ Script Learn >python reflection 01.py Start

Server is starting ...


Modify the above script as follows:

#/usr/bin/env python

#coding: UTF8


Import Sys


Class WebServer (object):

def __init__ (Self,host,port):

Self.host = Host

Self.port = Port


def start (self):

Print "Server is starting ..."


def stop (self):

Print "Server is stopping ..."


def restart (self):

Self.stop ()

Self.start ()


if __name__ = = "__main__":

Server = WebServer (' localhost ', 80)

If Hasattr (Server,sys.argv[1]):

Func = GetAttr (server,sys.argv[1]) # #获取server. Start memory address

Func () # #相当于server. Start ()

The execution script results are as follows:

D:\Python learn \ Script Learn >python reflection 02.py Start

Server is starting ...


D:\Python learn \ Script Learn >python reflection 02.py Stop

Server is stopping ...


Modify the above script as follows:

#/usr/bin/env python

#coding: UTF8


Import Sys


Class WebServer (object):

def __init__ (Self,host,port):

Self.host = Host

Self.port = Port


def start (self):

Print "Server is starting ..."


def stop (self):

Print "Server is stopping ..."


def restart (self):

Self.stop ()

Self.start ()


def test_run (name):

Print "Running ...", name


if __name__ = = "__main__":

Server = WebServer (' localhost ', 80)

If Hasattr (Server,sys.argv[1]):

Func = GetAttr (server,sys.argv[1]) # #获取server. Start memory address

Func () # #相当于server. Start ()


SetAttr (server, ' run ', Test_run) # #test_run以run身份绑定到server实例中

Server.run (' Peng ') # #类似执行test_run (' Peng ')


The execution script results are as follows:

D:\Python learn \ Script Learn >python reflection 03.py Start

Server is starting ...

Running ... Peng


Modify the above script as follows:

#/usr/bin/env python

#coding: UTF8


Import Sys


Class WebServer (object):

def __init__ (Self,host,port):

Self.host = Host

Self.port = Port


def start (self):

Print "Server is starting ..."


def stop (self):

Print "Server is stopping ..."


def restart (self):

Self.stop ()

Self.start ()


def test_run (self,name):

Print "Running ...", name,self.host


if __name__ = = "__main__":

Server = WebServer (' localhost ', 80)

If Hasattr (Server,sys.argv[1]):

Func = GetAttr (server,sys.argv[1]) # #获取server. Start memory address

Func () # #相当于server. Start ()


SetAttr (server, ' run ', Test_run) # #test_run以run身份绑定到server实例中

Server.run (server, ' Peng ') # #类似执行test_run (' Peng ')

The execution script results are as follows:

D:\Python learn \ Script Learn >python reflection 04.py Start

Server is starting ...

Running ... peng localhost


Modify the above script as follows:

#/usr/bin/env python

#coding: UTF8


Import Sys


Class WebServer (object):

def __init__ (Self,host,port):

Self.host = Host

Self.port = Port


def start (self):

Print "Server is starting ..."


def stop (self):

Print "Server is stopping ..."


def restart (self):

Self.stop ()

Self.start ()


def test_run (self,name):

Print "Running ...", name,self.host


if __name__ = = "__main__":

Server = WebServer (' localhost ', 80)

If Hasattr (Server,sys.argv[1]):

Func = GetAttr (server,sys.argv[1]) # #获取server. Start memory address

Func () # #相当于server. Start ()


Delattr (WebServer, ' start ')

Print Server.restart ()



#setattr (server, ' run ', Test_run) # #test_run以run身份绑定到server实例中

#server. Run (server, ' Peng ') # #类似执行test_run (' Peng ')

The execution script results are as follows:

D:\Python Learning \ Script Learning >python reflection 05.py Restart

Server is stopping.---Executes a call to Func ()

Server is starting ... ---"Executes a call to Func ()

Server is stopping ... ---is executing a call to Server.restart ()

Traceback (most recent):

File "Reflection 05.py", line +, in <module>

Print Server.restart ()

File "Reflection 05.py", line, in restart

Self.start ()

Attributeerror: ' WebServer ' object has no attribute ' start ' ---executed by the call of Server.restart () , at which start has been deleted, so reported "' WebServer ' object has no attribute ' start ' ERROR!!!

This article is from a "a little" blog, make sure to keep this source http://pengai.blog.51cto.com/6326789/1933012

The reflection of Python learning

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.