Reflection of Python functions

Source: Internet
Author: User

The following files are available:

index.py

#!/usr/bin/env python
#-*-Coding:utf-8-*-
__author__ = ' Ryan '
"""
Import Home
print ' Oldboy .... '
Url=raw_input (' URL: ')
If url = = ' Home/dev ':
ret = Home.dev ()
Print ret
If url = = '/home/index ':
ret = Home.index ()
Print ret
If url = = '/home/user ':
RET = Home.user
Print ret
If url = = '/home/passwd ':
RET = HOME.PASSWD
Print ret
Else
print ' 404 '
"""
#getattr, SetAttr, Delattr, hasattr
#找到home文件, load the content into memory, and then GetAttr, SetAttr, Delattr, hasattr to the home interrupt function
#由于函数在没有执行之前, just put the function name into memory
Import Home
#print Dir (Home)
#print hasattr (home, ' Dev ') #通过函数hasattr到内存中找到home模块, determine if there is a dev function under the Home module, return true, otherwise false
#print Dir (Home)
#print getattr (home, ' Dev ') #到内存中的home模块中去获取函数dev (in fact, get the dev function in memory address)
#print setattr (Home, ' Alex ', ' gentle Man ') #通过setattr对内存中的home模块添加一个字符串alex
#print Dir (Home)
#delattr (home, ' Dev ') #通过delattr删除内存中home模块中的dev函数
#print Dir (Home)
#
"""
Class Foo:
Static_name= ' NBA '
def __init__ (self):
Self.name= ' Alex '
Def show (self):
Pass
@staticmethod
def static_show (self):
Pass
@classmethod
def class_show (CLS):
Pass
#obj =foo ()
#print Foo.__dict__.keys () #查看类里面的成员
#print hasattr (Foo, ' static_show ')
#print obj.__dict__# See which members are in the object
#print hasattr (obj, ' name ')
#print hasattr (obj, ' show ') #普通方法是存储在类里, but here we use the HASATTR function to find out if there is a Show method in Object obj and return ture, The reason is that after the object obj is not found, it is further looked up through the object pointer to the class that created the object, so this returns true
"""
"""
Print hasattr (Foo, ' __init__ ')
Print "######### #分隔符 ①################"
Print hasattr (Foo, ' static_name ')
Print "######### #分隔符 ②################"
Print hasattr (Foo, ' show ')
Print "######### #分隔符 ③################"
Print hasattr (Foo, ' static_show ')
Print "######### #分隔符 ④################"
Print hasattr (Foo, ' class_show ')
Obj=foo ()
Print obj.__dict__
Print hasattr (obj, ' show ')
Print hasattr (obj, ' __init__ ')
Print "######### #分隔符 ①################"
Print hasattr (obj, ' static_name ')
Print "######### #分隔符 ②################"
Print hasattr (obj, ' show ')
Print "######### #分隔符 ③################"
Print hasattr (obj, ' static_show ')
Print "######### #分隔符 ④################"
Print hasattr (obj, ' class_show ')
"""
"""
Import Home
CLS = GetAttr (Home, "Foo")
Print CLS
S_name = GetAttr (CLS, ' Static_name ')
Print S_name
C_show = GetAttr (CLS, ' Show ')
Print C_show
S_show=getattr (CLS, ' Static_show ')
Print S_show
Cla_show = GetAttr (CLS, ' Class_show ')
Print Cla_show
"""
Import Home
CLS = GetAttr (CLS, ' Foo ')
Obj=cls () #实例化一个对象obj
Name = GetAttr (obj, ' name ')
Print Name

home.py

#!/usr/bin/env python
#-*-Coding:utf-8-*-
__author__ = ' Ryan '
def index ():
Return ' result Home.index '
Def Dev ():
Return ' result Home.dev '
def user ():
Return ' result Home.user '
def passwd ():
Return ' result home.passwd '
"""
Class Foo:
Static_name= ' NBA '
def __init__ (self):
Self.name= ' Alex '
Def show (self):
Pass
@staticmethod
def static_show (self):
Pass
@classmethod
def class_show (CLS):
Pass
"""

Reflection

fanshe.py

#!/usr/bin/env python
#-*-Coding:utf-8-*-
__author__ = ' Ryan '
url = raw_input (' URL: ')
Controller,action =url.split ('/')
Import Home
#action动作是一个字符串, go to a container (module) to find the function, the string is the function name, if any, get the function
Func =getattr (home,action)
func = Dev Dev1 ():
Return ' result Home.dev '
ret = func ()
#func =getattr (Home, ' index ')
Ret=func ()
Print ret


webdemo.py

#!/usr/bin/env python#coding:utf-8from wsgiref.simple_server import make_serverdef  Runserver (Environ, start_response):     start_response (' 200 OK ',  [(' Content-type ',  ' text/html ')]     url = environ[' PATH_INFO ']     temp = url.split ('/') [1]    import home     #去home模块中检查, whether it contains the specified function     is_exist = hasattr (home, temp)       #如果存在指定的函数     if is_exist:         #获取函数         func = getattr (home, temp)           #执行函数并获取返回值         ret = func ( )          #将函数返回值响应响应给请求者, ie browser          return ret    else:         #如果不存在函数则返回给浏览器 ' 404 not found '         return  ' 404 not found ' if __name__ ==   ' __main__ ':     httpd = make_server (',  8001, runserver)      print  "serving http on port 8001 ..."     httpd.serve_forever ()


This article from "Flat Light is true" blog, please be sure to keep this source http://ucode.blog.51cto.com/10837891/1763328

Reflection of Python functions

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.