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 :: python range function 
Python :: python error catching of modules 
Python :: caqch làm app chatbot python 
Python :: intersection of list of sets 
Python :: color to black and white opencv 
Python :: python classmethod property 
Python :: write a variable and assin a string to it 
Python :: jupiter output 
Python :: how to put 2 code n 1 line in python 
Python :: show every second xtick 
Python :: delete csr python 
Python :: recover dict from 0-d numpy array 
Python :: divide all the numbers of a list by one number python 
Python :: Using a generic exception block 
Python :: what is python virtual environment 
Python :: dataset analysis in python photo photoelectric effect 
Python :: remove punctuation in dataframe column 
Python :: how to remove zero after decimal float python 
Python :: pandas df where 
Python :: upper python 
Python :: python move 
Python :: odd and even python 
Python :: pandas integer to date 
Python :: create set in python 
Python :: list input python 
Python :: numpy square root 
Python :: runserver coomand in django 
Python :: python tuple methods 
Python :: phone numbers 
Python :: how to make python print 2 line text in one code 
ADD CONTENT
Topic
Content
Source link
Name
2+8 =