python attribute code example
Example 1: python class get attribute by name
>>> class c:
pass
o = c()
>>> setattr(o, "foo", "bar")
>>> o.foo
'bar'
>>> getattr(o, "foo")
'bar'
Example 2: attributes in python
class Monkey(object):
def __init__(self, name):
self.name = name
def speak(self):
print("Hello! I am " + self.name)
IronMan = Monkey('TonyStark')
IronMan.speak()
Example 3: 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)