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 :: how to concatenate a string with int in python 
Python :: open excel through python 
Python :: merge multiple excel workssheets into a single dataframe 
Python :: drop colums whoose value are object type in python 
Python :: Write a program that prints #pythoniscool, followed by a new line, in the standard output. Your program should be maximum 2 lines long You are not allowed to use print or eval or open or import sys in your file 
Python :: django form list option 
Python :: python install minio 
Python :: python see if a number is greater than other 
Python :: Find Files With a Certain Extension in the Directory and Its Subdirectories in Python 
Python :: how to get the type of numpy array 
Python :: convert timedelta to days 
Python :: print from within funciton with multiprocessing 
Python :: reorder columns pandas 
Python :: get last 3 things in a list python 
Python :: if else in list comprehension 
Python :: get_absolute_url django 
Python :: split by several characters python 
Python :: Find unique values in all columns in Pandas DataFrame 
Python :: group by 2 unique attributes pandas 
Python :: prevent division by zero numpy 
Python :: python get element from dictionary 
Python :: how to get median mode average of a python list 
Python :: get user django 
Python :: django datepicker 
Python :: how to check for a substring in python 
Python :: link in embed discord.py 
Python :: read data from s3 bucket python 
Python :: unique combinations in python 
Python :: rock paper scissors python 
Python :: python beginner projects 
ADD CONTENT
Topic
Content
Source link
Name
2+4 =