Python Forum

Full Version: What is all the info in the info window in Idle?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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)
Python has a rich data-model: https://docs.python.org/3/reference/data...-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)
Output:
[MyFoo: 10, MyFoo: 9, MyFoo: 8, MyFoo: 7, MyFoo: 6, MyFoo: 5, MyFoo: 4, MyFoo: 3, MyFoo: 2, MyFoo: 1, MyFoo: 0] __lt__ called __lt__ called __lt__ called __lt__ called __lt__ called __lt__ called __lt__ called __lt__ called __lt__ called __lt__ called [MyFoo: 0, MyFoo: 1, MyFoo: 2, MyFoo: 3, MyFoo: 4, MyFoo: 5, MyFoo: 6, MyFoo: 7, MyFoo: 8, MyFoo: 9, MyFoo: 10]
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
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.
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__
Output:
AttributeError: 'Foo' object has no attribute '__dict__
Dataclasses supports this too:
from dataclasses import dataclass

@dataclass(slots=True)
class Foo2:
    x: int
    y: int
If you use a class with slots, you cannot add new attributes to the class.