Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

print all attributes of object python

# use dir method 
import random

print(dir(random))

['BPF', 'LOG4', 'NV_MAGICCONST', 'RECIP_BPF', 'Random', 'SG_MAGICCONST',
'SystemRandom', 'TWOPI', '_BuiltinMethodType', '_MethodType', '_Sequence',
'_Set', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__',
'__name__', '__package__', '__spec__', '_acos', '_ceil', '_cos', '_e', '_exp',
'_inst', '_log', '_pi', '_random', '_sha512', '_sin', '_sqrt', '_test', '_test_generator',
'_urandom', '_warn', 'betavariate', 'choice', 'expovariate', 'gammavariate', 'gauss',
'getrandbits', 'getstate', 'lognormvariate', 'normalvariate', 'paretovariate', 'randint',
'random', 'randrange', 'sample', 'seed', 'setstate', 'shuffle', 'triangular', 'uniform',
'vonmisesvariate', 'weibullvariate']

It will return a list  where you can find attribute ;

Example : we can use random.choice(list) , choice  is in list so we can 
  		use it with random .  

Comment

Print object attributes in python

# listing out all attributes of an object in Python
# create a class
class Student:
    def __init__(self, name, id, age):
        self.name = name
        self.id = id
        self.age = age

    def info(self):
        return f"Student name: {self.name} and id: {self.id} "

    # class variable
    var = 1


# creating object
s1 = Student("Mradula", 1, 20)
s2 = Student("Joel", 2, 15)
print(s1.info())
print(s1.__dir__())
Comment

Print object attributes in python

# listing out all attributes of an object in Python
# create a class
class Student:
    def __init__(self, name, id, age):
        self.name = name
        self.id = id
        self.age = age

    def info(self):
        return f"Student name: {self.name} and id: {self.id} "

    # class variable
    var = 1


# creating object
s1 = Student("Mradula", 1, 20)
s2 = Student("Joel", 2, 15)
print(s1.info())
print(s1.__dir__())
Comment

PREVIOUS NEXT
Code Example
Python :: discord py edit message 
Python :: graph 3d python 
Python :: pandas group by day 
Python :: python dictionary rename key 
Python :: python add string and int 
Python :: how to import from parent directory 
Python :: python access global variable 
Python :: create a blank image cv2 
Python :: how to get key of a particular value in dictionary python using index 
Python :: how to play mp3 files using vlc python library 
Python :: discord.py how get user input 
Python :: max of three numbers in python 
Python :: string to bits python 
Python :: how to get today weekday in python 
Python :: pip in vscode linux 
Python :: best python ide for ubuntu 
Python :: dockerfile for django project 
Python :: count number of each item in list python 
Python :: how to calculate z score in python 
Python :: run calc.exe inside python 
Python :: pandas datetime from date month year columns 
Python :: how to remove a tuple from a list python 
Python :: time 
Python :: heatmap of pandas dataframe with seaborn 
Python :: binary, decimal, hex conversion python 
Python :: python merge pdf files into one 
Python :: keras lstm example 
Python :: virtual env python 2 
Python :: pandas dict from row 
Python :: how to install packages inside thepython script 
ADD CONTENT
Topic
Content
Source link
Name
8+2 =