Python Forum
What is all the info in the info window in Idle?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What is all the info in the info window in Idle?
#1
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)
Reply
#2
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
Pedroski55 likes this post
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
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.
Reply
#4
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.
Pedroski55 likes this post
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Input network device connection info from data file edroche3rd 6 1,062 Oct-12-2023, 02:18 AM
Last Post: edroche3rd
  Is there a way to call and focus any popup window outside of the main window app? Valjean 6 1,796 Oct-02-2023, 04:11 PM
Last Post: deanhystad
  Pyspark Window: perform sum over a window with specific conditions Shena76 0 1,184 Jun-13-2022, 08:59 AM
Last Post: Shena76
  [split] Py2exe Writing UNKNOWN-0.0.0-py3.7.egg-info sarahroxon7 1 944 Apr-20-2022, 08:02 AM
Last Post: VadimCr
  Taking info from Json & using it in Python Extra 8 2,356 Apr-02-2022, 04:45 PM
Last Post: Extra
  Hiding "undesired" info Extra 4 1,801 Jan-03-2022, 08:25 PM
Last Post: Extra
  Looking for data/info on a perticular data-proccesing problem. MvGulik 9 3,907 May-01-2021, 07:43 AM
Last Post: MvGulik
  IDLE editing window no longer works chris1 2 2,225 Feb-06-2021, 07:59 AM
Last Post: chris1
  Need info for getting directory content in Windows wlathan 2 1,967 May-20-2020, 12:40 PM
Last Post: wlathan
  Py2exe Writing UNKNOWN-0.0.0-py3.7.egg-info Rickus 2 3,688 Feb-18-2020, 03:09 PM
Last Post: Rickus

Forum Jump:

User Panel Messages

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