Почему тип данных в классе Element это tuple, если обращаться через "container" всё в порядке, только при обращении на прямую tuple

Рейтинг: 0Ответов: 1Опубликовано: 15.07.2023
import requests


class Element:

    def __init__(self,
                 number,
                 symbol,
                 atom_mass,
                 short_electron_config,
                 element_type,
                 melting_temperature,
                 boiling_temperature,
                 electronegativity,
                 valence,
                 oxidation_states,
                 atom_radius,
                 covalent_radius,
                 van_der_waals_radius,
                 discover_year,
                 stp_density,
                 liquid_density,
                 vickers_hardness,
                 brinell_hardness,
                 mohs_hardness,
                 electron_affinity,
                 thermal_conductivity,
                 electric_conductivity,
                 universe_abundance,
                 crust_abundance,
                 ocean_abundance,
                 solar_abundance,
                 meteor_abundance,
                 human_abundance,
                 specific_heat,
                 vaporization_heat,
                 fusion_heat,
                 ionization_energy,
                 full_electron_config,
                 energy_levels,
                 quantum_numbers,
                 bulk_module,
                 young_module,
                 shear_module,
                 isotope_count,
                 ):
        self.number = int(number),
        self.symbol = symbol,
        self.atom_mass = atom_mass,
        self.short_electron_config = short_electron_config,
        self.element_type = element_type,
        self.melting_temperature = melting_temperature,
        self.boiling_temperature = boiling_temperature,
        self.electronegativity = electronegativity,
        self.valence = valence,
        self.oxidation_states = oxidation_states,
        self.atom_radius = atom_radius,
        self.covalent_radius = covalent_radius,
        self.van_der_waals_radius = van_der_waals_radius,
        self.discover_year = discover_year,
        self.stp_density = stp_density,
        self.liquid_density = liquid_density,
        self.vickers_hardness = vickers_hardness,
        self.brinell_hardness = brinell_hardness,
        self.mohs_hardness = mohs_hardness,
        self.electron_affinity = electron_affinity,
        self.thermal_conductivity = thermal_conductivity,
        self.electric_conductivity = electric_conductivity,
        self.universe_abundance = universe_abundance,
        self.crust_abundance = crust_abundance,
        self.ocean_abundance = ocean_abundance,
        self.solar_abundance = solar_abundance,
        self.meteor_abundance = meteor_abundance,
        self.human_abundance = human_abundance,
        self.specific_heat = specific_heat,
        self.vaporization_heat = vaporization_heat,
        self.fusion_heat = fusion_heat,
        self.ionization_energy = ionization_energy,
        self.full_electron_config = full_electron_config,
        self.energy_levels = energy_levels,
        self.quantum_numbers = quantum_numbers,
        self.bulk_module = bulk_module,
        self.young_module = young_module,
        self.shear_module = shear_module,
        self.isotope_count = isotope_count,
        self.container = (number,
                          symbol,
                          atom_mass,
                          short_electron_config,
                          element_type,
                          melting_temperature,
                          boiling_temperature,
                          electronegativity,
                          valence,
                          oxidation_states,
                          atom_radius,
                          covalent_radius,
                          van_der_waals_radius,
                          discover_year,
                          stp_density,
                          liquid_density,
                          vickers_hardness,
                          brinell_hardness,
                          mohs_hardness,
                          electron_affinity,
                          thermal_conductivity,
                          electric_conductivity,
                          universe_abundance,
                          crust_abundance,
                          ocean_abundance,
                          solar_abundance,
                          meteor_abundance,
                          human_abundance,
                          specific_heat,
                          vaporization_heat,
                          fusion_heat,
                          ionization_energy,
                          full_electron_config,
                          energy_levels,
                          quantum_numbers,
                          bulk_module,
                          young_module,
                          shear_module,
                          isotope_count,)

    def __repr__(self):
        return str(self.container)


url = "https://ptable.com/JSON/properties-90d5338.json"
periodic_table_json = requests.get(url).json()
periodic_table_json.remove({'isotopes': 1})
periodic_table = {}


def catcher(element_, path_1, path_2=''):
    try:
        if path_2:
            try:
                return float(element_[path_1][path_2])
            except (ValueError, TypeError):
                return element_[path_1][path_2]
        else:
            try:
                return float(element_[path_1])
            except (ValueError, TypeError):
                return element_[path_1]

    except KeyError:
        return 'N/A'


for element in periodic_table_json:

    periodic_table[element['symbol']] = Element(
        number=element['atomic'],
        symbol=element['symbol'],
        atom_mass=element['weight'],
        short_electron_config=element['electronstring'],
        element_type=element['series'],
        melting_temperature=catcher(element, 'melt'),
        boiling_temperature=catcher(element, 'boil'),
        electronegativity=catcher(element, 'electroneg'),
        valence=catcher(element, 'valence'),
        oxidation_states=catcher(element, 'oxidation'),
        atom_radius=catcher(element, 'radius', 'calculated'),
        covalent_radius=catcher(element, 'radius', 'covalent'),
        van_der_waals_radius=catcher(element, 'radius', 'vanderwaals'),
        discover_year=element['discover'],
        stp_density=catcher(element, 'density', 'stp'),
        liquid_density=catcher(element, 'density', 'liquid'),
        vickers_hardness=catcher(element, 'hardness', 'vickers'),
        brinell_hardness=catcher(element, 'hardness', 'brinell'),
        mohs_hardness=catcher(element, 'hardness', 'mohs'),
        electron_affinity=catcher(element, 'affinity'),
        thermal_conductivity=catcher(element, 'conductivity', 'thermal'),
        electric_conductivity=catcher(element, 'conductivity', 'electric'),
        universe_abundance=element['abundance']['universe'],
        crust_abundance=catcher(element, 'abundance', 'crust'),
        ocean_abundance=catcher(element, 'abundance', 'ocean'),
        solar_abundance=catcher(element, 'abundance', 'solar'),
        meteor_abundance=catcher(element, 'abundance', 'meteor'),
        human_abundance=catcher(element, 'abundance', 'human'),
        specific_heat=catcher(element, 'heat', 'specific'),
        vaporization_heat=catcher(element, 'heat', 'vaporization'),
        fusion_heat=catcher(element, 'heat', 'fusion'),
        ionization_energy=catcher(element, 'ionize'),
        full_electron_config=element['expandedconfig'],
        energy_levels=element['electrons'],
        quantum_numbers=element['quantum'],
        bulk_module=catcher(element, 'modulus', 'bulk'),
        young_module=catcher(element, 'modulus', 'shear'),
        shear_module=catcher(element, 'modulus', 'young'),
        isotope_count=element['isotopes'],
    )


print(type(periodic_table['H'].atom_radius))

Почему тип данных при таком обращении - print(type(periodic_table['H'].atom_radius)) это tuple? И в функции написано, что float и при обращении через .container это float, только при прямом обращении возвращается tuple. .atom_radius и 'H' я взял для примера так со всеми элементами и со всеми их свойствами.

Ответы

▲ 4Принят

У вас в конструкторе много лишних запятых, из-за чего все свойства становятся кортежами. Пример:

self.symbol = symbol,
self.atom_mass = atom_mass,

Здесь, вместо обычного присвоения вы присваиваете кортеж. Это всё равно, что написать:

self.symbol = (symbol,)
self.atom_mass = (atom_mass,)

Просто уберите запятые:

class Element:

    def __init__(self,
                 number,
                 symbol,
                 atom_mass,
                 short_electron_config,
                 element_type,
                 melting_temperature,
                 boiling_temperature,
                 electronegativity,
                 valence,
                 oxidation_states,
                 atom_radius,
                 covalent_radius,
                 van_der_waals_radius,
                 discover_year,
                 stp_density,
                 liquid_density,
                 vickers_hardness,
                 brinell_hardness,
                 mohs_hardness,
                 electron_affinity,
                 thermal_conductivity,
                 electric_conductivity,
                 universe_abundance,
                 crust_abundance,
                 ocean_abundance,
                 solar_abundance,
                 meteor_abundance,
                 human_abundance,
                 specific_heat,
                 vaporization_heat,
                 fusion_heat,
                 ionization_energy,
                 full_electron_config,
                 energy_levels,
                 quantum_numbers,
                 bulk_module,
                 young_module,
                 shear_module,
                 isotope_count,
                 ):
        self.number = int(number)
        self.symbol = symbol
        self.atom_mass = atom_mass
        self.short_electron_config = short_electron_config
        self.element_type = element_type
        self.melting_temperature = melting_temperature
        self.boiling_temperature = boiling_temperature
        self.electronegativity = electronegativity
        self.valence = valence
        self.oxidation_states = oxidation_states
        self.atom_radius = atom_radius
        self.covalent_radius = covalent_radius
        self.van_der_waals_radius = van_der_waals_radius
        self.discover_year = discover_year
        self.stp_density = stp_density
        self.liquid_density = liquid_density
        self.vickers_hardness = vickers_hardness
        self.brinell_hardness = brinell_hardness
        self.mohs_hardness = mohs_hardness
        self.electron_affinity = electron_affinity
        self.thermal_conductivity = thermal_conductivity
        self.electric_conductivity = electric_conductivity
        self.universe_abundance = universe_abundance
        self.crust_abundance = crust_abundance
        self.ocean_abundance = ocean_abundance
        self.solar_abundance = solar_abundance
        self.meteor_abundance = meteor_abundance
        self.human_abundance = human_abundance
        self.specific_heat = specific_heat
        self.vaporization_heat = vaporization_heat
        self.fusion_heat = fusion_heat
        self.ionization_energy = ionization_energy
        self.full_electron_config = full_electron_config
        self.energy_levels = energy_levels
        self.quantum_numbers = quantum_numbers
        self.bulk_module = bulk_module
        self.young_module = young_module
        self.shear_module = shear_module
        self.isotope_count = isotope_count
        self.container = (number,
                          symbol,
                          atom_mass,
                          short_electron_config,
                          element_type,
                          melting_temperature,
                          boiling_temperature,
                          electronegativity,
                          valence,
                          oxidation_states,
                          atom_radius,
                          covalent_radius,
                          van_der_waals_radius,
                          discover_year,
                          stp_density,
                          liquid_density,
                          vickers_hardness,
                          brinell_hardness,
                          mohs_hardness,
                          electron_affinity,
                          thermal_conductivity,
                          electric_conductivity,
                          universe_abundance,
                          crust_abundance,
                          ocean_abundance,
                          solar_abundance,
                          meteor_abundance,
                          human_abundance,
                          specific_heat,
                          vaporization_heat,
                          fusion_heat,
                          ionization_energy,
                          full_electron_config,
                          energy_levels,
                          quantum_numbers,
                          bulk_module,
                          young_module,
                          shear_module,
                          isotope_count)

    def __repr__(self):
        return str(self.container)