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 :: download csv file from jupyter notebook 
Python :: django convert object to dict 
Python :: Python Frozenset operations 
Python :: xls in python 
Python :: pyqt5 qtextedit change color of a specific line 
Python :: python generate set of random numbers 
Python :: pytest local modules 
Python :: numpy sqrt 
Python :: tf MaxPooling2D 
Python :: how to install python in ubuntu 
Python :: opencv black white image 
Python :: Python Date object to represent a date 
Python :: python range in intervals of 10 
Python :: python print emoji 
Python :: set python 3 as default mac 
Python :: layer enable time arcpy 
Python :: write in entry() in tkinter 
Python :: selenium get h1 text python 
Python :: python sleep timer 
Python :: win64pyinstaller 
Python :: how to return a value from a function in python 
Python :: map python 3 
Python :: numpy savetxt list of strings 
Python :: python replace with something else 
Python :: swapping variables in python 
Python :: python lambda function if else 
Python :: how to hide ticks marks in plot 
Python :: decode multipart/form-data python lambda 
Python :: flask bootstrap 
Python :: python how to sum two lists 
ADD CONTENT
Topic
Content
Source link
Name
1+4 =