hash, sum, super, idlen, max, min, next, sumdelattr, dir, getattr, globals, locals, setattr, varsascii, format, reprinput, open, print __import__breakpoint, @classmethod, compile, eval, exec, help, @staticmethod
Draft for Information Only
Content
Python Built-in Misc. Functions delattr() Parameters Remarks dir() Parameters Remarks getattr() Parameters Remarks globals() Parameters Remarks locals() Parameters Remarks setattr() Parameters Remarks vars() Parameters Remarks Source and Reference
Python Built-in Misc. Functions
The Python interpreter has some built-in misc. property functions.
delattr()
delattr(object, name)
Parameters
delattr()to delete the specified attribute from the given object.
objectto specify the object to be delete from
nameto specify the attribute name to be deleted.
Remarks
object is an object.
name is a string of the attribute name
name must be the name of one of the object's attributes
delattr deletes the named attribute, provided the object allows it.
dir()
dir([object])
Parameters
dir()to return a name list.
[object]optional, to specify the object to be returned from
Remarks
- If
object is omitted, dir returns the list of names in the current local scope.
- If
object is specified, dir returns a list of valid attributes for the object
- If
object has a __dir__() method, this method will be called and must return the list of attributes. This allows objects that implement a custom __getattr__() or __getattribute__() function to customize the way dir() reports their attributes.
- If the object does not provide
__dir__(), the function tries its best to gather information from the object's __dict__ attribute, if defined, and from its type object. The resulting list is not necessarily complete, and may be inaccurate when the object has a custom __getattri__().
- The default
dir() mechanism behaves differently with different types of objects, as it attempts to produce the most relevant, rather than complete, information:
- If the object is a module object, the list contains the names of the module's attributes
- If the object is a type or class object, the list contains the names of its attributes, and recursively of the attributes of its bases.
- Otherwise, the list contains the object's attributes' names, the names of its class's attributes, and recursively of the attributes of its class's base classes.
- Because
dir() is supplied primarily as a convenience for use at an interactive prompt, it tries to supply an interesting set of names more than it tries to supply a rigorously or consistently defined set of names, and its detailed behavior may change across releases. For example, metaclass attributes are not in the result list when the argument is a class.
getattr()
getattr(object, name[, default])
Parameters
getattr()to return the specified name attribute of an object.
objectto specify the object to be returned from
nameto specify the name attribute to be returned from
defaultoptional, to specify the default attribute when name attribute does not exist.
Remarks
getattr() returns the value of the name attribute of object,
name must be a string. If the string is the name of one of the object's attributes, the result is the value of that attribute.
getattr(object, 'name') is equivalent to object.name
- If the named attribute does not exist,
defualt is returned if providede, otherwise attributeError is raised.
globals()
globals()
Parameters
globals()to return a globals dictionary representing the current global symbol table.
Remarks
- This is always the dictionary of the current module (inside a function or method, this is the module where it is defined, not the module from which it is called).
locals()
locals()
Parameters
locals()to return and update the dictionary representing the current local symbol table.
Remarks
- Free variables are returned by
locals when it is called in function blocks, but not in class blocks.
- Note that at the module level,
locals() and globals() are the same dictionary
- The contents of this dictionary should not be modified; changes may not affect the values of local and free variables used by the interpreter.
setattr()
setattr(object, name, value)
Parameters
setattr()to set the value of the attribute of an object.
objectto specify the object to be returned from
nameto specify the attribute to be assigned to
valueto specify the attribute value to be used for assigning
Remarks
- This is the counterpart of getattr(). The arguments are an object, a string and an arbitrary value. The string may name an existing attribute or a new attribute. The function assigns the value to the attribute, provided the object allows it.
vars()
vars([object])
Parameters
vars()to return the type of an object.
[object]optional, to specify the object to be returned from.
Remarks
- Return the __dict__ attribute for a module, class, instance, or any other object with a __dict__ attribute.
Objects such as modules and instances have an updateable __dict__ attribute; however, other objects may have write restrictions on their __dict__ attributes (for example, classes use a types.MappingProxyType to prevent direct dictionary updates).
Without an argument, vars() acts like locals(). Note, the locals dictionary is only useful for reads since updates to the locals dictionary are ignored.
Source and Reference
©sideway
ID: 201102902 Last Updated: 11/29/2020 Revision: 0
|
|