Python hasattr() Function26 Sept 2024 | 1 min read The python hasattr() function returns true if an object has given named attribute. Otherwise, it returns false. SignatureParametersobject: It is an object whose named attribute is to be checked. attribute: It is the name of the attribute that you want to search. ReturnIt returns true if an object has given named attribute. Otherwise, it returns false. Python hasattr() Function Example 1The below example shows the working of hasattr(). Output: Employee has age?: True Employee has salary?: False Explanation: In the above example, we have created a class named as Employee, then we create the object of the Employee class, i.e., employee. The hasattr(employee, 'age') returns the true value as the employee object contains the age named attribute, while hasattr(employee, 'salary')) returns false value as the employee object does not contain the salary named attribute. Next TopicPython Functions |
Function function returns the reversed iterator of the given sequence. Signature reversed(sequence) Parameters sequence: It is a sequence that needs to be reversed. Return It returns the reversed iterator of the given sequence. Function Example 1 The above example shows the working of reversed for a sequence: string, tuple, list, and range....
1 min read
Function function is a constructor which creates a dictionary. Python dictionary provides three different constructors to a create dictionary. If no argument is passed, it creates an empty dictionary. If a positional argument is given, a dictionary is created with the same key-value pairs. Otherwise, pass an...
1 min read
Function The python iter() function is used to return an iterator object. It creates an object which can be iterated one element at a time. Signature iter(object, sentinel) Parameters object: An iterable object. sentinel (optional)- It is a special value that represents the end of a sequence. Return It returns an iterator object. ...
1 min read
Function The python format() function returns a formatted representation of the given value. Signature format(value, format) Parameters value: It is the value that needs to be formatted. format: It is the specification on how the value should be formatted. Return It returns a formatted representation of the given value. Function Example 1 The...
1 min read
Function Python has() function is used to get the hash value of an object. Python calculates the hash value by using the hash algorithm. The hash values are integers an used to compare dictionary keys during a dictionary lookup. We can hash only these types: Hashable types:...
2 min read
Function function is used to get the smallest element from the collection. This function takes two arguments, first is a collection of elements and second is key and returns the smallest element from the collection. Signature min (collection[, key]) Parameters collection: It is a comma-separated list of elements. key: Specifies...
1 min read
Function The python bytearray() function returns a bytearray object and can convert objects into bytearray objects, or create an empty bytearray object of the specified size. Signature bytearray(x, encoding, error) Parameters x (optional) : It is the source that initializes the array of bytes. encoding (optional) : It is an...
1 min read
Function In python, a set is a built-in class, and this function is a constructor of this class. It is used to create a new set using elements passed during the call. It takes iterable as an argument and returns a new set object. The...
1 min read
Function The python open() function opens the file and returns a corresponding file object. Signature open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None) Parameters file: It is a path like object giving the pathname of the file to be opened. mode (optional): It specifies the mode in which the file is...
2 min read
Function The python float() function returns a floating-point number from a number or string. A floating-point number, or float, is a mathematical value that contains a decimal point. Floats can be positive or negative and can address both whole numbers and fractional values. In Python, the...
2 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India