Answers for "python attribute"

2

python class get attribute by name

>>> class c:
        pass
o = c()
>>> setattr(o, "foo", "bar")
>>> o.foo
'bar'
>>> getattr(o, "foo")
'bar'
Posted by: Guest on July-01-2020
2

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 ....
Posted by: Guest on April-10-2021

Python Answers by Framework

Browse Popular Code Answers by Language