Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #98455

python PEP suggestion

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Dan Strohl <D.Strohl@F5.com>
Newsgroups comp.lang.python
Subject python PEP suggestion
Date Fri, 6 Nov 2015 18:21:23 +0000
Lines 96
Message-ID <mailman.130.1446996920.16136.python-list@python.org> (permalink)
Mime-Version 1.0
Content-Type text/plain; charset="us-ascii"
Content-Transfer-Encoding quoted-printable
X-Trace news.uni-berlin.de tRg2q+vEphTngS6SDDnRgg/MXDiOBmfM6GzfbDEuJ5og==
Return-Path <D.Strohl@f5.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'elif': 0.04; '"""': 0.05; 'needed,': 0.05; 'objects,': 0.07; 'returned.': 0.07; 'subject:PEP': 0.07; 'type,': 0.07; 'dict': 0.09; 'falls': 0.09; 'str,': 0.09; 'subclass': 0.09; 'throw': 0.09; 'will,': 0.09; 'def': 0.13; 'subject:python': 0.14; '(json,': 0.16; 'json,': 0.16; 'json.dumps,': 0.16; 'objet': 0.16; 'overview:': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'thoughts?': 0.16; 'try:': 0.18; 'all,': 0.20; 'developer': 0.20; 'first,': 0.20; 'to:name:python-list@python.org': 0.20; 'converted': 0.22; 'object.': 0.22; 'pass': 0.22; 'trying': 0.22; '(or': 0.23; 'developers': 0.26; 'define': 0.27; 'handling': 0.27; 'converting': 0.27; 'function': 0.28; 'this.': 0.28; 'arguments,': 0.29; 'dan': 0.29; 'forms,': 0.29; 'if,': 0.29; 'pep': 0.29; 'received:192.168.10': 0.29; 'types.': 0.29; 'convert': 0.29; 'objects': 0.29; 'raise': 0.29; 'allows': 0.30; 'classes': 0.30; 'extend': 0.31; 'another': 0.32; 'skip:_ 10': 0.32; 'operate': 0.32; 'run': 0.33; 'point': 0.33; 'class': 0.33; 'point,': 0.33; 'structure': 0.34; 'except': 0.34; 'list': 0.34; 'so,': 0.35; 'could': 0.35; 'done': 0.35; '???': 0.35; 'attempt': 0.35; 'unknown': 0.35; 'something': 0.35; 'but': 0.36; 'list,': 0.36; 'skip:i 20': 0.36; 'should': 0.36; 'there': 0.36; 'url:org': 0.36; 'possible.': 0.36; 'possible': 0.36; 'basic': 0.36; 'form,': 0.36; 'to:addr:python-list': 0.36; 'two': 0.37; 'skip:& 10': 0.37; 'method': 0.37; 'suggestion': 0.37; 'thought': 0.37; 'url:rec- html40': 0.37; 'charset:us-ascii': 0.37; 'wanted': 0.37; 'doing': 0.38; '(with': 0.38; 'skip:o 20': 0.38; 'goes': 0.39; 'data': 0.39; 'format': 0.39; 'received:192': 0.39; 'to:addr:python.org': 0.40; 'url:schemas': 0.40; 'url:office': 0.40; 'flat': 0.63; 'wall': 0.63; 'more': 0.63; 'different': 0.63; 'information': 0.63; 'cast': 0.66; 'worth': 0.67; 'reply': 0.68; 'wish': 0.71; 'special': 0.73; 'proposal:': 0.84; 'received:192.168.15': 0.84; 'enhancement': 0.96
DKIM-Signature v=1; a=rsa-sha256; c=simple/simple; d=f5.com; i=@f5.com; q=dns/txt; s=seattle; t=1446834156; x=1478370156; h=from:to:subject:date:message-id:mime-version; bh=OIYn1MpfyP04IF0By/IjC2Vqu2c6UDh3yk3SvkMNn8Q=; b=OOVKlHwMfAFFjiSBfXYVygRsXuMN0EsjPqkY2Jl+fw+ANUwh8t+xee99 5cRk/0cv8UMzUat8G9J+0REG3WnreoqwczahnXU+gLxmpXh/t9bG8PpEl 6SbVQR/F41UrlRhRrm9+VRupNoaSksdFhlqqVzVDYWOce3f9+oGxCLKXt Y=;
X-IronPort-AV E=Sophos;i="5.20,253,1444694400"; d="scan'208,217";a="187116579"
X-IPAS-Result A2GeBAAy7zxW/+sKqMBeGQEDDwEBAQEGAQEBAYI0gSB1wAkHEgUBh38BAQEBAQEEfAuEPC1eAYEAJgEEG8lLDCGGVIw5DEGBMQWNG4ktjR6cS4RnhH+BBwEBAQ
Thread-Topic python PEP suggestion
Thread-Index AdEXU30AJvZJdlJcRrKQ33nHaSS7QA==
Accept-Language en-US
Content-Language en-US
X-MS-Has-Attach
X-MS-TNEF-Correlator
x-ms-exchange-transport-fromentityheader Hosted
x-originating-ip [192.168.15.239]
X-Mailman-Approved-At Sun, 08 Nov 2015 10:35:19 -0500
X-Content-Filtered-By Mailman/MimeDel 2.1.20+
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:98455

Show key headers only | View raw


All,

I wanted to run the following thought past the list as a possible PEP enhancement suggestion to see if it feels like something that is worth proposing.   I know it is not in the PEP format at this point, I can, and will, clean it up if needed, I am just trying to throw it against the wall at this point to see if it resonates... (or if it falls flat and goes "splat" <grin>).

Thoughts?

Dan Strohl



New special method name to allow for more flexible object type casting/access, and extend type() to cast objects using this special method name.

Overview:

Have a new special method name that would allow a given objects to request information from another object in a given type, or to cast an object into a different type, and extend the built in type() function to use this.

Rationale:
There is currently __str__, __int__, and __bool__ that allow me to tell an object how it should reply to a request for these types of basic data types.  However if I want to access a copy of the objet in dict form, or as a list, or if I am trying to convert something using json, there is no standard way of doing that for a custom object (I know I can do __getitem__ and/or __iter__, but I many processes don't try these if the object is not a subclass of dict or list/tuple)

Proposal:
What I am proposing is something like:

object.__cast__(self, to_class):
                              """
                              to_class: the class type that you wish to return.
                              """

               -and-

               Class type(object, to_class):
                              """
                              With two arguments, will attempt to cast "object" into "to_class" if possible.   This would be done by something like the following:
                              (with 1 and 3 arguments, will work as currently designed)
                              """
                              Type(object, to_class):
                                             If isinstance(to_class, (int, str, bool)):
                                                            Return to_class(object)
                                             Else:
                                                            Try:
                                                                           Return object.__cast__(to_class):
                                                            Except AttributeError:
                                                                           # yes, I know this should be more readable!
                                                                           Raise TypeError('object x could not be converted to type y")



This allows for more customization on how a developer would want to return this object in various forms, and if, for example, the custom object was passed to something like json.dumps, it could try converting the object to something it recognizes first, or even try doing something like type(custom_object, json) and see what returned.

So, in implementation I might do something like:

def __conv__(self, to_class):
               if isinstance(to_class, (json, dict)):
                              return self._data_dict
               elif isinstance(to_class, ElementTree):
                              return self._get_xml_dict()
               else:
                                             raise TypeError('could not convert object to class')


This allows for developers of classes that operate on data passed to be able to define a process that they would use to accept unknown objects,  without having to worry about handling all of the different potential object types, and pass the responsibility for how to structure the information to the developer of the custom object.


Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

python PEP suggestion Dan Strohl <D.Strohl@F5.com> - 2015-11-06 18:21 +0000
  Re: python PEP suggestion André Roberge <andre.roberge@gmail.com> - 2015-11-08 14:02 -0800

csiph-web