[Python-ideas] JavaScript-Style Object Creation in Python (using a constructor function instead of a class to create objects)

Stephan Houben stephanh42 at gmail.com
Wed May 17 14:08:24 EDT 2017


Hi Sven,

"I hope the second ': int' can be omitted because 0 already is an int."

0 is also an Any, an object, a SupportAbs, and a Union[int, str].
And infinitely more, of course.

A typechecker needs to be explicitly told which was intended.

Stephan

Op 17 mei 2017 19:52 schreef "Sven R. Kunze" <srkunze at mail.de>:

Hi Stephan,
hi Ivan,


On 17.05.2017 18:48, Ivan Levkivskyi wrote:

from typing import NamedTuple

class Foo(NamedTuple):
    """Foo is a very important class and
    you should totally use it.
    """
    bar: int
    baz: int = 0

    def grand_total(self):
        return self.bar + self.baz

typing.NamedTuple supports docstrings, user-defined methods, and default
values.


I hope the second ': int' can be omitted because 0 already is an int.


This makes me wonder three things:
1) Michel, can newcomers differentiate between when to use ' : ' and when
to use ' = ' and a combination thereof?
2) There must be a lot of cornercases where people rewrite Foo to be a
normal class in the end, right?
3) If one doesn't need tuple-__dunder__ methods, a "normal" class would
even need 1 line less. (+ Stephan's second point)


So, this still leaves those missing __dunder__ magic methods doing the
right thing at the right time.


Sven
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20170517/0e0504db/attachment.html>


More information about the Python-ideas mailing list