python attributes code example

Example 1: get object attributes python

for att in dir(your_object):
    print (att, getattr(your_object,att))

Example 2: python object with attributes

class Object(object):
    pass

a = Object()
a.somefield = somevalue

Example 3: attributes in python

class Monkey(object):
  def __init__(self, name): # This will done automatically when cmd in 7th line will be executed
    self.name = name # This is an attribute.
  def speak(self): # This is a method
    print("Hello! I am " + self.name)
    
IronMan = Monkey('TonyStark')
IronMan.speak() # This will use the function 'speak' defind in class 'Monkey'
# Created By ....

Example 4: how to assign a variable to a class in python

class Shark:
    animal_type = "fish"
    location = "ocean"
    followers = 5

new_shark = Shark()
print(new_shark.animal_type)
print(new_shark.location)
print(new_shark.followers)