Skip to main content

Posts

Showing posts from 2020

getattrib, Setattrib, hasattrib and delattrib in python

# `getattr(object, name[, default])` Function in Python The `getattr(object, name[, default])` function returns the value of a named attribute of an object, where `name` must be a string. If the object has an attribute with the specified `name`, then the value of that attribute is returned. On the other hand, if the object does not have an attribute with `name`, then the value of `default` is returned, or `AttributeError` is raised if `default` is not provided. ```python >>> t = ('This', 'is', 'a', 'tuple') >>> t.index('is') 1 >>> getattr(t, 'index') <built-in method index of tuple object at 0x10c15e680> >>> getattr(t, 'index')('is') 1 ``` when the attribute is not defined, ```python >>> getattr(t, 'len') Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'tuple' object has no attribute 'len

Python tips

python tips: In [1]: os.listdir(os.getcwd())==os.listdir(os.curdir) Out[1]: True In [2]: os.listdir(os.getcwd())==os.listdir('.') Out[2]: True     ******* WAP for print a character its position times in a word. word=raw_input("Enter the word:") for i in word:      print i*word.index(i) Output: ****** WAP for print a character its ASCII values times in a word. word=raw_input("Enter the word:") for i in word: print i*ord(i) #alternatively # print [i*ord(i) for i in word]