Python Forum
I would like to know more about dunders
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I would like to know more about dunders
#1
Hello,

There are two tutorials that I can think of that I'd like to see:

1. dunders (also known as magic methods or the python data model) some of this
    (but far from all) would be covered by the decorators tutorial.

2. Use of option file with tkinter. This is rarely brought up, and not even known by many. It allows for
    great color themes and many other styles. It also allows for user customization, and finally keept the
    clutter out of GUI scripts. A lot of programmers think tkinter is passe, but it can actually be quite
    attractive when styles are used.
    I have been working on this one, hopefully I'll get far enough along to release it soon.

Larz60+
Reply
#2
(Sep-30-2016, 01:34 AM)Larz60+ Wrote: 1. dunders (also known as magic methods or the python data model) some of this
    (but far from all) would be covered by the decorators tutorial.

The basics of dunders are covered in Class Intermediate: Operator Overloading. Were you looking for something more detailed?
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Ichabod801,

I use dunders quite a bit, but sometimes not sure which ones I need to provide.
For example. in the following code that I wrote, I wasn't sure if I needed to raise StopIteration or not, or if I needed
to include __repr__ Snippsat didn't think I did when I first posted this (pyforum.io):
from collections import namedtuple
import json


class Record:
    def __init__(self, fname=None):
        self.rec = self.read_data_file(fname)
        self.maxitem = len(self.rec)

    def read_data_file(self, fname):
        with open(fname, 'r') as f:
            j = f.read()
        return json.loads(j, object_hook=lambda data: namedtuple('data', data.keys())(*data.values()))

    def __repr__(self):
        return self

    def __iter__(self):
        self.itemnum = 0
        return self

    def __next__(self):
        if self.itemnum > self.maxitem:
            raise StopIteration
        rec = self.rec[self.itemnum]
        self.itemnum += 1
        return rec


if __name__ == '__main__':
    fname = 'StockData.json'
    r = Record(fname)
    for record in r:
        print(record)
There's a pretty good write up in the Fluent Python book which I have been reading, need to read more, so I
will just absorb what comes along. I usually learn at least something new from all that I read.

I took a quick look at your post, and it looks very interesting, I'll read it tomorrow morning as that's when my brain works best. Getting old sucks!

Larz60+
Reply
#4
   def __repr__(self):
       return self
Wouldn't that create an error? __repr__ has to return a string, while this is part of an old style class, right?
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:01:18) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> class spam:
...   def __repr__(self):
...     return self
...
>>> x=spam()
>>> print(x)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: __str__ returned non-string (type spam)
>>>
Yep!
Reply
#6
print(x) uses x.__str__. Typing the variable and hitting return uses x.__repr__:

Python 2.7.12 (default, Jul  1 2016, 15:12:24) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> class spam:
...     def __repr__(self):
...             return self
... 
>>> x = spam()
>>> x
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: __repr__ returned non-string (type instance)
>>> repr(x)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: __repr__ returned non-string (type instance)
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#7
You are correct.

I was planning on doing something with printing record. This was a stub that should have been deleted.
Not sure what would happen if I tried to print from the class.
Reply


Forum Jump:

User Panel Messages

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