<<DEITEL - Python How to Program 2002>>Special attributes of an object of a class.0.0.0.3
2010-08-24 14:31
609 查看
Attribute Description
__class__ A reference to the class from which the object was instantiated.
__dict__ A dictionary that corresponds to the object’s namespace. Each keyvalue pair represents an identifier and its value in the namespace.
代码
Time1.Time
>>> print time1.__dict__
{'second': 0, 'minute': 0, 'hour': 0}
>>> print time1.doc
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: Time instance has no attribute 'doc'
>>> print time1.__doc__
Time abstract data type (ADT) definition
>>> print time1.__module__
Time1
>>>
__class__ A reference to the class from which the object was instantiated.
__dict__ A dictionary that corresponds to the object’s namespace. Each keyvalue pair represents an identifier and its value in the namespace.
代码
# Fig. 7.1: Time1.py # Simple definition of class Time. class Time: """Time abstract data type (ADT) definition""" def __init__( self ): """Initializes hour, minute and second to zero""" self.hour = 0 # 0-23 self.minute = 0 # 0-59 self.second = 0 # 0-59 def printMilitary( self ): """Prints object of class Time in military format""" print "%.2d:%.2d:%.2d" % ( self.hour, self.minute, self.second ) def printStandard( self ): """Prints object of class Time in standard format""" standardTime = "" if self.hour == 0 or self.hour == 12: standardTime += "12:" else: standardTime += "%d:" % ( self.hour % 12 ) standardTime += "%.2d:%.2d" % ( self.minute, self.second ) if self.hour < 12: standardTime += " AM" else: standardTime += " PM" print standardTime, ########################################################################## # (C) Copyright 2002 by Deitel & Associates, Inc. and Prentice Hall. # # All Rights Reserved. # # # # DISCLAIMER: The authors and publisher of this book have used their # # best efforts in preparing the book. These efforts include the # # development, research, and testing of the theories and programs # # to determine their effectiveness. The authors and publisher make # # no warranty of any kind, expressed or implied, with regard to these # # programs or to the documentation contained in these books. The authors # # and publisher shall not be liable in any event for incidental or # # consequential damages in connection with, or arising out of, the # # furnishing, performance, or use of these programs. # ##########################################################################
Time1.Time
>>> print time1.__dict__
{'second': 0, 'minute': 0, 'hour': 0}
>>> print time1.doc
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: Time instance has no attribute 'doc'
>>> print time1.__doc__
Time abstract data type (ADT) definition
>>> print time1.__module__
Time1
>>>
相关文章推荐
- How to Get a List of Class Attributes in Python
- How to determine the size of an object of class?
- How to cast List<Object> to List<MyClass> Object集合转换成实体集合
- Question 33: How can a C++ developer use the placement new syntax to make new allocate an object of class SomeClass at a particu
- pod install后出现: [!] `<PBXResourcesBuildPhase UUID=`x`>` attempted to initialize an object with an...
- 去除Warning C4251 “class 'std::vector<_Ty>' needs to have dll-interface to be used by clients of class
- How to get the published properties of an persistent object
- <转>[C#][Tutorial] How to become an EndScene() hooker
- java中的类型安全问题-Type safety: Unchecked cast from Object to ... 或者 Type safety: Unchecked cast from Type to Class<T>
- How to make a submit out of a <a href…>…</a> link?
- vue引入插件Cannot assign to read only property 'exports' of object '#<Object>'
- IronPython and LINQ to Objects (IV): 实现IEnumerable<Object>
- Cannot assign to read only property 'exports' of object '#<Object>' 解决方案
- Trying to clone an uncloneable object of class Imagic的解决方法
- Vue报错:Uncaught TypeError: Cannot assign to read only property’exports‘ of object’#<Object>‘的解决方法
- Trying to clone an uncloneable object of class Imagic的解决方法
- A good blog about how to write an Hadoop MapReduce program in Python
- python报TypeError: cannot convert the series to <class 'float'>
- org.hibernate.MappingException: An AnnotationConfiguration instance is required to use <mapping class="Bbs"/>
- Python - [ File Object ] <<>> [ File Descrcptor ]