![]() |
What is all the info in the info window in Idle? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: What is all the info in the info window in Idle? (/thread-40303.html) |
What is all the info in the info window in Idle? - Pedroski55 - Jul-07-2023 I only use Python for practical purposes, to do little jobs. I never found a task where I thought I need a class. Today I was just looking at a class, see below. When I write R. in Idle, a little window appears with append, data head. If I write R._ the little window contains a long list of things. What is all that info?? What can I do with it? class Rotating: """Rotate a list as new items are added. List stays the same length.""" def __init__(self, items): self.data = list(items) self.head = 0 def __iter__(self): for i in range(len(self)): yield self[i] def __getitem__(self, i): return self.data[(i + self.head) % len(self)] def __len__(self): return len(self.data) def append(self, item): self.data[self.head] = item self.head = (self.head + 1) % len(self) def __str__(self): return 'Rotating({})'.format( self.data[self.head:] + self.data[:self.head]) R = Rotating('abcdef') print(R) R.append('g') print(R) R.append('h') print(R) print(R[0], len(R), R[5]) print(R.data) RE: What is all the info in the info window in Idle? - DeaD_EyE - Jul-07-2023 Python has a rich data-model: https://docs.python.org/3/reference/datamodel.html#objects-values-and-types Some methods and attributes are created automatically. For example, you can add the ability of comparison to your class and then all instances are sortable. But after the creation of the class itself, it gets from object the attributes and methods assigned. For example, the attribute __name__ holds the name of the class. But you have never created it, it's done automatically.from functools import total_ordering # adds additional methods to the class MyFoo # for comparison @total_ordering class MyFoo: def __init__(self, value): self.value = value def __eq__(self, other): print("__eq__ called") return self.value == other.value def __lt__(self, other): print("__lt__ called") return self.value < other.value def __repr__(self): return f"MyFoo: {self.value}" foos = [MyFoo(x) for x in range(10, -1, -1)] print(foos) foos.sort() print(foos) There is another way with the use of dataclass .They implement those required methods with the decorator syntax. The same as a dataclass .from dataclasses import dataclass @dataclass(order=True) class MyFoo2: value: int RE: What is all the info in the info window in Idle? - Pedroski55 - Jul-07-2023 Thanks, I'll have to digest that first! Is it true that all class data are stored in Python dictionaries? I seem to have pick that up from what I've read. RE: What is all the info in the info window in Idle? - DeaD_EyE - Jul-08-2023 Not all classes do have a dict. If the classvariable __slots__ is used, then only the names from the supplied sequence are available and there is no __dict__ . This is used to save memory. If you have only one instance of the class, you don't care. If you have one million, you care.class Foo: __slots__ = ("x", "y") Foo().__dict__ Dataclasses supports this too:from dataclasses import dataclass @dataclass(slots=True) class Foo2: x: int y: intIf you use a class with slots, you cannot add new attributes to the class. |