Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

how to use setattr Python

class Food():
    breakfast = "Pancakes"
    Lunch = "Sandwich"
#Parts of setattr
#self: what class or object
#name: the variable you are affecting
#value: what the variable's value will be
setattr(Food,"breakfast","Toast")
print(Food.breakfast)
>>Toast
#You can also add a variable
setattr(Food,"Dinner","Steak")
print(Food.Dinner)
>>Steak
Comment

setattr python

# Per https://www.w3schools.com/python/ref_func_setattr.asp, 
# the setattr() function sets the value of the specified attribute of the specified object.

# Here is a sample code from https://www.w3schools.com/python/trypython.asp?filename=demo_ref_setattr
class Person:
  name = "John"
  age = 36
  country = "Norway"

setattr(Person, 'age', 40)

# The age property will now have the value: 40

x = getattr(Person, 'age')

print(x)
Comment

PREVIOUS NEXT
Code Example
Python :: pathlib path of current file 
Python :: learn python the hard way 
Python :: boxplot show values seaborn 
Python :: fibonacci 
Python :: print random integers python 
Python :: install anaconda python 2.7 and 3.6 
Python :: maximum and minimum value of array python 
Python :: cors python 
Python :: generate all combinatinosrs of a list pyton 
Python :: entropy formula pyhon 
Python :: filter a pandas dataframe by length of list in a column 
Python :: how to raise the exception in __exit__ python 
Python :: python if 
Python :: how to find unique values in list in python 
Python :: python dictionary 
Python :: cli args python 
Python :: Python RegEx Escape – re.escape() 
Python :: pandas change period to daily frequency 
Python :: matplotlib documentation download via 
Python :: python save to excel 
Python :: termcolor print python 
Python :: how to get circumference from radius 
Python :: python string to list of int 
Python :: python max function with lambda 
Python :: Copying a list using deepcopy() in python 
Python :: python matplotlib 
Python :: csv len python 
Python :: go to line in python 
Python :: transpose list 
Python :: pillow python text example 
ADD CONTENT
Topic
Content
Source link
Name
8+8 =