Some function to handle class and its interface info
2013-05-31 22:41
369 查看
1) get some class info
the following example show basic inheritance
There are some useful functions or methods to know about the inheritance infomation
2) handle Interface info
There are some useful fuction and method to check instance interface infomation.
The function callable is no longer available in Python 3.0. Instead of callable(x), you can use hasattr(x, '__call__').
The inverse of getattr is setattr, which can be used to set the attributes of an object:
if you want to see all the values store in an object, you can examines its __dict__ attrbute.
the following example show basic inheritance
class Filter: def init(self): self.blocked = [] def filter(self, sequence): return [x for x in sequence if x not in self.blocked] class SPAMFilter(Filter): # SPAMFilter is a subclass of Filter def init(self): # Overrides init method from Filter superclass self.blocked = ['SPAM']
There are some useful functions or methods to know about the inheritance infomation
>>> issubclass(SPAMFilter, Filter) #check subclass True >>> issubclass(Filter, SPAMFilter) False >>> SPAMFilter.__bases__ #show baseclass info (<class __main__.Filter at 0x171e40>,) >>> Filter.__bases__ () >>> s = SPAMFilter() >>> isinstance(s, SPAMFilter) #check instance info True >>> isinstance(s, Filter) True >>> isinstance(s, str) False >>> s.__class__ #show class info <class __main__.SPAMFilter at 0x1707c0>
2) handle Interface info
There are some useful fuction and method to check instance interface infomation.
>>> tc = Filter() >>> hasattr(tc, 'filter') #check whether the required methods are present. True >>> hasattr(tc, 'haha') False >>> callable(getattr(tc, 'filter', None)) # check whether the talk attribute was callable True >>> callable(getattr(tc, 'haha', None)) False
The function callable is no longer available in Python 3.0. Instead of callable(x), you can use hasattr(x, '__call__').
The inverse of getattr is setattr, which can be used to set the attributes of an object:
>>> setattr(tc, 'newAttr', 'xixi') >>> tc.newAttr 'xixi'
if you want to see all the values store in an object, you can examines its __dict__ attrbute.
>>>SPAMFilter.__dict__ {'__module__': '__main__', 'init': <function init at 0xb71ffa74>, '__doc__': None} >>>Filter.__dict__ {'filter': <function filter at 0xb71ffaac>, '__module__': '__main__', 'init': <function init at 0xb71ff72c>, '__doc__': None}
相关文章推荐
- How do I declare and use a pointer to a class member function?
- it has a DefiningQuery and no InsertFunction element exists in the ModificationFunctionMapping element to support the current op
- (ZT) Is it legal (and moral) for a member function to say delete this?
- About TortoiseGit TortoiseGit is a Windows Shell Interface to Git and based on TortoiseSVN. It's ope
- This function has none of Deterministic,no sql,or reads sql data in its declaration and binary logging is enabled(you *might* want to use the less safe log_bin_trust_function_creators variable
- asm[dynamically produce a class and use it without writing it to disk]
- How to write a first class paper and get it published in a scientific journal?
- How to define a template class in a .h file and implement it in a .cpp file
- Use C# to get JSON Data from the Web and Map it to .NET Class => Made Easy! 转
- Warning: phpinfo() [function.phpinfo]: It is not safe to rely on the system's timezone settings.
- intermediate javascript: assign a function with its parameter to a variable and execute later
- Unable to update the EntitySet 'T_JsAPI' because it has a DefiningQuery and no <InsertFunction> element exists in the <ModificationFunctionMapping> element to support the current operation.
- I learned several ASP.NET's AJAX ability today! It is so interesting and so easy to use AJAX in ASP.NET.
- The important thing in life is to have a great aimand the determination to attain it.
- CocoaPods问题: CocoaPods was not able to update the `master` repo.if it is a unexpected issue and
- A function to help graphical model checks of lm and ANOVA(转)
- how to make Completion Handlers and use it in Swift
- IE title changes to <afterHash> if the page has a url with '#' , and has flash/swf embedded in it
- Discover Feature Engineering, How to Engineer Features and How to Get Good at It
- httpurlconnection - How to use java.net.URLConnection to fire and handle HTTP requests? - Stack Overflow