Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

how to add attribute to class 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

python add attribute to class instance

setattr(objectClass, "newAttribute", "new attr")
Comment

PREVIOUS NEXT
Code Example
Python :: plt text matplotlib white background 
Python :: create an empty numpy array and append 
Python :: flatten tf keras 
Python :: def extract_title(input_df): 
Python :: python get the app path 
Python :: dataframe select columns based on list 
Python :: how to add percentage in countplot 
Python :: ord python 
Python :: merging df vertically 
Python :: python negative indexing 
Python :: how to create an entry box on tkinter python 
Python :: how to make program speak in python 
Python :: python cholesky 
Python :: get last 3 elements in a list python 
Python :: find all regex matches python 
Python :: numpy savetext 
Python :: python 1 line for loop with else 
Python :: input in python 
Python :: how to slice a string in python 
Python :: numpy divide with exception 
Python :: huggingface dataset from pandas 
Python :: tweepy aut code 
Python :: Program for length of the shortest word 
Python :: python csv writer row by row 
Python :: maximum and minimum value of array python 
Python :: prolog avg of list 
Python :: python dictionary to list 
Python :: Python program to combine each line from first file with the corresponding line in second file 
Python :: python download file from ftp 
Python :: how to alight and place ipywidgets 
ADD CONTENT
Topic
Content
Source link
Name
8+2 =