Advantages of Default Factory in Dataclasses

2021-11-16 Thread Abdur-Rahmaan Janhangeer
Greetings list,

A simple question: why do we need field(default_factory ) in dataclasses?

Why not make that field as an attribute return a function?

Useful implementation examples / use cases appreciated.

Kind Regards,

Abdur-Rahmaan Janhangeer
about  | blog

github 
Mauritius
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Advantages of Default Factory in Dataclasses

2021-11-16 Thread Paul Bryan
On Tue, 2021-11-16 at 17:04 +0400, Abdur-Rahmaan Janhangeer wrote:

> A simple question: why do we need field(default_factory ) in
> dataclasses?

To initialize a default value when a new instance of the dataclass is
created. For example, if you want a field to default to a dict. A new
dict is created for each instance of the dataclass created.

> Why not make that field as an attribute return a function?

I do not understand the question.

> Useful implementation examples / use cases appreciated.

Any case where a you want a dataclass field to default to a mutable
value. Examples: dicts, lists, other dataclasses.

Paul

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Advantages of Default Factory in Dataclasses

2021-11-16 Thread dn via Python-list
On 17/11/2021 02.04, Abdur-Rahmaan Janhangeer wrote:
> A simple question: why do we need field(default_factory ) in dataclasses?
> 
> Why not make that field as an attribute return a function?
> 
> Useful implementation examples / use cases appreciated.


It's an interesting question: We could define a list (or whatever)
within __post_init__()!

Greater minds than mine may care to comment on the theory that something
defined as a dataclasses.field will be allocated as part of the class
constructor, ie lower-cost. Whereas, something defined post-init will
require extra process-time and storage-allocation. (?)

Doesn't it already return a (evaluated) function? Remember: one is not
limited to Python built-in or PSL types. Thus:

def countdown():
return [ 10, 9, 8, 7, ..., "blast-off" ]

...
launch_commentary:list = field( default_factory=countdown )


The use of default-values for mutables is something of a Python
'gotcha'. My use-case is that some wise-soul's decision to do things
this way prevents me from falling into that debugging "slough of despond".
(does such qualify as a "use case"?)
-- 
Regards,
=dn
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Advantages of Default Factory in Dataclasses

2021-11-16 Thread David Lowry-Duda
On Tue, Nov 16, 2021 at 05:04:05PM +0400, Abdur-Rahmaan Janhangeer wrote:
> A simple question: why do we need field(default_factory ) in 
> dataclasses?

For the same reason that the following code doesn't do what some people 
might expect it to:

```python
def add_to(elem, inlist=[]):
inlist.append(elem)
return inlist

list1 = add_to(1)
list2 = add_to(2)
print(list1)  # prints [1]
print(list2)  # prints [1, 2], potentially confusing
```

Mutable attributes are created once, upon definition. Reusing the same 
function (or instantiating objects of the same class) and modifying this 
attribute can lead to possibly unexpected side effects as above.

- DLD
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: One line sort

2021-11-16 Thread Christian Gollwitzer

Am 15.11.21 um 14:10 schrieb ast:

A curiosity:

q = lambda x: x and q([i for i in x[1:] if i < x[0]]) + [x[0]] + q([i 
for i in x[1:] if i >= x[0]])


 >>> q([7, 5, 9, 0])
[0, 5, 7, 9]


That seems to be a translation of the classic Haskell quicksort example:

qsort [] = []
qsort (x:xs) = qsort [a | a <- xs, a < x]
  ++ [x] ++
   qsort [b | b <- xs, b >= x]


The Haskell version is a bit clearer IMHO due to the pattern matching, 
but that results in a 2 liner :)


Christian
--
https://mail.python.org/mailman/listinfo/python-list


Re: Alternatives to Jupyter Notebook

2021-11-16 Thread Martin Schöön
Den 2021-11-15 skrev Loris Bennett :
> Martin Schöön  writes:
>
>> Den 2021-10-20 skrev Shaozhong SHI :
>>>
>>> My Jupyter notebook becomes unresponsive in browsers.
>>>
>> Odd, I never had any problems like that. I use Firefox on Linux.
>>
>>> Are there alternatives to read, edit and run Jupyter Notebook?
>>>
>> I know some people use emacs orgmode. I have never tried it
>> myself and do not know how well it works.
>
> I don't know Jupyter well enough to know whether Org mode is a real
> alternative.  However, with Org mode you can combine text and code in
> multiple languages, run the code within Emacs and then export the
> results in various formats such as PDF and HTML.
>
I realise I was not making myself clear. Orgmode is great. Full stop.

What I don't have experience of is combining orgmode with Jupyter.

/Martin
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Advantages of Default Factory in Dataclasses

2021-11-16 Thread Alan Bawden
David Lowry-Duda  writes:

   ...

   For the same reason that the following code doesn't do what some people 
   might expect it to:

   ```python
   def add_to(elem, inlist=[]):
   inlist.append(elem)
   return inlist

   list1 = add_to(1)
   list2 = add_to(2)
   print(list1)  # prints [1]
   print(list2)  # prints [1, 2], potentially confusing
   ```

Not only does it not print what "most people" expect.  It also doesn't
print what _you_ expect!  (But you made your point.)
-- 
https://mail.python.org/mailman/listinfo/python-list