Python Forum
For loop in my __init__ doesn't work as expected
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
For loop in my __init__ doesn't work as expected
#1
 class DictionnaireOrdonne:
        """Classe permettant de créer un dictionnaire ordonné comme une liste, avec des indices"""

        def __init__(self, **keys_values):
            """classe prennent un nombre indéfinit de paramètres nommées"""

            self.keys_values = keys_values
            self._dictionnaire = self.keys_values
            for keys in self.keys_values:
                self.keys = keys

        def __repr__(self):
            """Cette méthode est appelée quand on appelle l'objet"""

            return str(self.keys_values)

        def __getitem__(self, key):
            """Cette méthode spéciale est appelée quand on fait objet[index]
                    Elle redirige vers self._dictionnaire[index]"""

            return self._dictionnaire[key]

        def __setitem__(self, key, value):
            """Cette méthode est appelée quand on écrit objet[index] = valeur
                    On redirige vers self._dictionnaire[index] = valeur"""

            self._dictionnaire[key] = value


test = DictionnaireOrdonne(one=1, two=2, three=3)
print(test.keys)
When I run this code, I get:
Output:
three
I only get one of the 3 keys, and it's the last one, i don't know why.

However, when if I modify my for loop to this:
for keys in self.keys_values:
        print(keys)
Then I get:
Output:
one two three
I get the output wanted, but I can't use it because it will print itself without me calling it.

Why do I get only get one key in my first example, and how can I make it so that I get my three keys when I call test.keys?
Reply


Messages In This Thread
For loop in my __init__ doesn't work as expected - by Jessy - Nov-18-2019, 02:07 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Why doesn't calling a parent constructor work with arbitrary keyword arguments? PurposefulCoder 4 964 Jun-24-2023, 02:14 PM
Last Post: deanhystad
  While Loop Does Not Work Properly mactron 4 931 Jun-22-2023, 01:04 AM
Last Post: mactron
  Why doesn't this code work? What is wrong with path? Melcu54 7 1,821 Jan-29-2023, 06:24 PM
Last Post: Melcu54
  color code doesn't work harryvl 1 900 Dec-29-2022, 08:59 PM
Last Post: deanhystad
  client.get_all_tickers() Doesn't work gerald 2 1,727 Jun-16-2022, 07:59 AM
Last Post: gerald
  pip doesn't work after Python upgrade Pavel_47 10 4,245 May-30-2022, 03:31 PM
Last Post: bowlofred
  Python class doesn't invoke setter during __init__, not sure if's not supposed to? mtldvl 2 3,354 Dec-30-2021, 04:01 PM
Last Post: mtldvl
  For Loop Works Fine But Append For Pandas Doesn't Work knight2000 2 2,033 Dec-18-2021, 02:38 AM
Last Post: knight2000
  Class Method to Calculate Age Doesn't Work gdbengo 1 1,719 Oct-30-2021, 11:20 PM
Last Post: Yoriz
  Process doesn't work but Thread work ! mr_byte31 4 2,643 Oct-18-2021, 06:29 PM
Last Post: mr_byte31

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020