Data Class Fields in Python














































Data Class Fields in Python



DATA CLASS FIELDS IN PYTHON
In the dataclasses module, there's a field function that allows to provide field-specific configuration is:
Syntax-
dataclasses.field(*, default=MISSING, default_factory=MISSING, repr=True, hash=None, init=True, compare=True, metadata=None)
The MISSING value is a sentinel object used to detect if the default and default_factory parameters are provided. This sentinel is used because None is a valid value for default. No code should directly use the MISSING value.
Example: Demonstration of how to view the fields of a data class object.
from dataclasses import dataclass
# A class for holding a student content
@dataclass
class Student:
# Attributes Declaration
# using Type Hints
name: str
roll no: int
course: int
# object of the class
s = Student(
"Harry",101,'Btech')
s.__dataclass_fields__

output-


The parameters to field() are:
default
default_factory
init
repr
hash
compare
metadata
Explanation of the parameters-

1. Default- When no value is provided during object creation, this parameter specifies the default value for the attribute. This default parameter is needed because the field call itself replaces the normal position of the default value.
EXAMPLE-
from dataclasses import dataclass, field
# A class for holding a person content
@dataclass
class Person:
name: str
age: int
city: str = field(default=
'Hyderabad')
# A DataClass object
p = Person(
"john", "20")
print(p)

output-


2. default_factory-  when this field is provided, it must be a zero-argument callable that will be called when a default value is needed for this field.
EXAMPLE-
from dataclasses import dataclass, field
# A class for holding a student content
from random import choice
def get_default_Subject():
Subject = [
'Maths', 'Science', "English"]
return choice(Subject)
@dataclass
class Student:
name: str
rollno: int
Subject: str = field(default_factory = get_default_Subject)
# A DataClass object
s = Student(
"Harry",202)
print(s)

output-


The above code puts one of the Maths, Science, or English as the default value for the subject while DataClass object creation.

3. init: If true this field is included as a parameter to the generated __init__() method.
EXAMPLE-
from dataclasses import dataclass, field
# A class for holding a student content
@dataclass
class Student:
# Attributes Declaration
# using Type Hints
name: str
course: str
# init field
rollno: int
DOB: str = field(init=
False, default="13/08/2000")
# object of dataclass
s = Student(
"Harry", "MBA", 113)
print(s)

output-


4. repr: If this is true (the default), this field is included in the string returned by the generated __repr__() method.
EXAMPLE-
from dataclasses import dataclass, field
# A class for holding an student content
@dataclass
class Student:
# Attributes Declaration
# using Type Hints
name: str
rollno: int
course: str
DOB: str = field(init=
False, default="12/07/1999", repr=True)
s = Student(
"John",303, "Bcom")
print(s)

output-


5. compare: If true (the default), this field is included in the generated equality and comparison methods (__eq__(), __gt__().
EXAMPLE-
from dataclasses import dataclass, field
# A class for holding a student content
@dataclass(unsafe_hash=True)
class Student:
# Attributes Declaration
# using Type Hints
name: str
rollno: int
DOB: str = field(init=
False, default="14/03/1998",
repr=
True, hash=False, compare=True)
s1 = Student(
"Harry",21)
s2 = Student(
"john",22)
print(s1 == s2)

output-



6. hash: If true, this field is included in the generated __hash__() method.
EXAMPLE-
from dataclasses import dataclass, field
# A class for holding an student content
@dataclass(unsafe_hash=True)
class Student():
name: str
course: str
roll_no: int = field(hash=
False)
s = Student(
'Harry','Btech', 102)
print(hash(s))

output-


If false then it will not consider these fields.

7. metadata: This is a dictionary, the key-value pair indicating various information and its data. This can be a mapping or none. None is treated as an empty dict.
EXAMPLE-
from dataclasses import dataclass, field
# A class for holding a student content
@dataclass
class Student:
course: str = field(compare=
False)
name: str = field(metadata={
'data': 'Student profile'})
DOB: str = field(default=
'13/04/1993')
rollno: int = field(init=
False, default=101)
# A DataClass object
s = Student(
"DataClass", "Harry")
print(s)
print(s.__dataclass_fields__[
'name'].metadata)
output-






Comments