Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python dataclass default factory

from dataclasses import dataclass, field

@dataclass
class Fruits():
    # names : list = []  # ValueError: mutable default <class 'list'> for field names is not allowed: use default_factory
    names : list = field(default_factory=lambda : [])


f1 = Fruits()
f1.names.append('Apple')
f1.names.append('Banana')
print(f1)      # Fruits(names=['Apple', 'Banana'])


f2 = Fruits(['Peach', 'Pear'])
print(f2)      # Fruits(names=['Peach', 'Pear'])
Comment

python dataclass default factory

from dataclasses import dataclass, field

@dataclass
class Fruits():
    # names : list = []  # ValueError: mutable default <class 'list'> for field names is not allowed: use default_factory
    names : list = field(default_factory=lambda : [])


f1 = Fruits()
f1.names.append('Apple')
f1.names.append('Banana')
print(f1)      # Fruits(names=['Apple', 'Banana'])


f2 = Fruits(['Peach', 'Pear'])
print(f2)      # Fruits(names=['Peach', 'Pear'])
Comment

PREVIOUS NEXT
Code Example
Python :: urlencode python 
Python :: python print int in string with zero padding 
Python :: how to launch an application using python 
Python :: argparse list 
Python :: python GOOGLE_APPLICATION_CREDENTIALS 
Python :: python get lan ip 
Python :: mongodb group by having 
Python :: python slice an array 
Python :: redirect to previous page django 
Python :: python list of all tkinter events 
Python :: handler.setLevel(logging.DEBUG) not working python 
Python :: max of matrix numpy 
Python :: select all columns except one pandas 
Python :: date to day python 
Python :: create virtualenv in linux python 
Python :: pandas replace zero with blank 
Python :: python 1 to 01 
Python :: how to increase bar width in python matplogtlib 
Python :: how to use python to sleep if the user is not using the system 
Python :: remove emoji from dataframe 
Python :: tkinter how to connect keyboard key to button 
Python :: decrease hours in datetime python 
Python :: auto python to exe 
Python :: how to add value to to interger in python 
Python :: execute command in python script 
Python :: update print python 
Python :: sns palette 
Python :: append method linked list python 
Python :: pytest loop 
Python :: how to append data to csv file in python without replacing the already present text 
ADD CONTENT
Topic
Content
Source link
Name
8+5 =