News for hackers. Stuff that matters.
Re:Python-patterns – A collection of design patterns/idioms ... by TekMol on Tuesday May 31, 2022 @02:18AM (#31566610)
I wish there was a pattern to encapsulate data and functions from a module.

So you could do:

    import greeter

    greet1 = encapsulate(greeter)
    greet2 = encapsulate(greeter)

    greet1.name = 'Joe'
    greet2.name = 'Sue'

    greet1.greet()
    greet2.greet()
And greeter.py would look like this:

    name = 'nobody'
    
    def greet():
        print ('Hello '+name)
Output:

    Hello Joe
    Hello Sue
This would be so much nicer and leaner than classes.
Re:Python-patterns – A collection of design patterns/idioms ... by roelschroeven on Tuesday May 31, 2022 @06:01AM (#31567727)
You want something that encapsulates state and a method. That's precisely what classes are for! I feel going against how the language is meant to so is just going to make things more difficult for yourself.

Is using modules like that nicer? That's subjective, I guess. I don't think it's nicer. You could use dataclasses to make classes somewhat nicer (though that is subjective too).

It it leaner than classes? I don't think it's something to worry about. Maybe use __slots__ in your class if and where needed. Is an encapsulated module still leaner than a class?

In the end both modules and classes are namespaces. Modules are singletons, classes have as many instances as you need.

Re:Python-patterns – A collection of design patterns/idioms ... by stinos on Tuesday May 31, 2022 @09:13AM (#31569086)
Idiomatic for me would be this simple, short, clear thing:

   def greeter(name):
     def greet():
       print(f"Hello {name}")
     return greet

   greet1 = greeter('Joe')
   greet2 = greeter('Sue')

   greet1()
   greet2()
Or else a class. I don't quite understand why you'd want a global in a module not mean the canonical 'this singletone thingie configures this module' ?
Re:Python-patterns – A collection of design patterns/idioms ... by born2discover on Tuesday May 31, 2022 @02:33PM (#31573093)
IMHO that greeter function of yours is unnecessarily complicated. The same could be achieved in an easier way:

"""python

from functools import partial

def greeter(name):

    print(f"Hello {name}")

greet1 = partial(greeter, "Joe")

greet2 = partial(greeter, "Sue")

greet1()

greet2()

"""

Re:Python-patterns – A collection of design patterns/idioms ... by zo1 on Tuesday May 31, 2022 @05:43AM (#31567615)
I don't understand, this looks like a class? Put all that in a class called Greeter in greeter.py and call it a day. Anything beyond that, I'd question very heavily "Why?" to justify the complexity/non-standardness.
Re:Python-patterns – A collection of design patterns/idioms ... by CraigJPerry on Tuesday May 31, 2022 @04:17AM (#31567192)
Here's my attempt:

      ~/Code/local/hn ································ at  09:14:14
     python3 hn_example.py       
    Hello Joe
    Hello Sue

      ~/Code/local/hn ································ at  09:14:16
     cat hn_example.py greeter.py
    ───────┬───────────────────────────────────────────────────────────
           │ File: hn_example.py
    ───────┼───────────────────────────────────────────────────────────
       1   │ import sys
       2   │ from importlib.util import find_spec, module_from_spec
       3   │ 
       4   │ def encapsulate(mod):
       5   │     s = find_spec(mod)
       6   │     m = module_from_spec(s)
       7   │     s.loader.exec_module(m)
       8   │     return m
       9   │ 
      10   │ greet1 = encapsulate("greeter")
      11   │ greet2 = encapsulate("greeter")
      12   │ 
      13   │ greet1.name = 'Joe'
      14   │ greet2.name = 'Sue'
      15   │ 
      16   │ greet1.greet()
      17   │ greet2.greet()
    ───────┴───────────────────────────────────────────────────────────
    ───────┬───────────────────────────────────────────────────────────
           │ File: greeter.py
    ───────┼───────────────────────────────────────────────────────────
       1   │ name = 'nobody'
       2   │     
       3   │ def greet():
       4   │     print ('Hello '+name)
    ───────┴───────────────────────────────────────────────────────────
I differ from your syntax by passing string parameters on (your) line 3 & 4 (my line 10 & 11).

I think this isn't a good idea though, i'd lookup __slots__ if leanness is an issue.

Re:Python-patterns – A collection of design patterns/idioms ... by TekMol on Tuesday May 31, 2022 @05:13AM (#31567452)
That is super awesome!

Do you think it negatively affects performance?

Why don't you think it's a good idea?

I don't see how __slots__ would be relevant to this? Isn't __slots__ a thing to define how classes store their properties?

Re:Python-patterns – A collection of design patterns/idioms ... by CraigJPerry on Tuesday May 31, 2022 @06:35AM (#31567942)
>> Do you think it negatively affects performance?

Yes and no. Pragmatically, no, because in python usually performance comes from (ignoring using native code) avoiding dynamic dispatches (circa 500x speedups are common) and like in almost all languages, being aware of your allocations (varies wildly but avoiding allocs in loops for example will give remarkable speedups).

In theory though, yes absolutely this is slower - what was a query of a dict (or even better, an array in the __slots__ case) - i.e. grabbing an attribute from a class - is now going outside the process and asking the OS about stuff on the filesystem. Masssively slower.

>> Why don't you think it's a good idea?

Because you’re paying an overhead to use something in an unexpected way when the language provides a faster, idiomatic solution to this problem:

>> a pattern to encapsulate data and functions

That’s the class and object system described in one sentence

>> I don't see how __slots__ would be relevant to this?

You’ve heard of turtles all the way down? In python it’s dicts all the way down. Behind the scenes you can think of a loaded module being a dict, just the same as an object has a dict underpinning it.

Slots comes in as an optimisation on this dicts all the way down. An optimisation that’s not open to you if you (ab?)use modules like this.

But i’d finish up by saying this - i’m a random person on the internet that doesn’t know your use case. I stand by the above as decent general case advice but you might actually have a valid case to deviate AND you’re not going to unlock new discoveries in better ways to do things if you just always follow the dogma so don’t be put off by my negativity toward the idea but DO measure the outcomes you care about when experimenting to see if the modules idea is working for you.

Re:Python-patterns – A collection of design patterns/idioms ... by TekMol on Tuesday May 31, 2022 @07:52AM (#31568483)
If I understand you correctly, only the creation of greet1 and greet2 are slower, but then the usage of these objects is just as fast as using instances of classes?
Re:Python-patterns – A collection of design patterns/idioms ... by CraigJPerry on Tuesday May 31, 2022 @12:25PM (#31571474)
Yeah, I was pretty sure that was the case but just to be certain:

    >>> from dis import dis
    >>> dis(greet)
      4           0 LOAD_GLOBAL              0 (print)
                  2 LOAD_CONST               1 ('Hello ')
                  4 LOAD_GLOBAL              1 (name)
                  6 BINARY_ADD
                  8 CALL_FUNCTION            1
                 10 POP_TOP
                 12 LOAD_CONST               0 (None)
                 14 RETURN_VALUE
    >>> dis(greet1.greet)
      4           0 LOAD_GLOBAL              0 (print)
                  2 LOAD_CONST               1 ('Hello ')
                  4 LOAD_GLOBAL              1 (name)
                  6 BINARY_ADD
                  8 CALL_FUNCTION            1
                 10 POP_TOP
                 12 LOAD_CONST               0 (None)
                 14 RETURN_VALUE
Re:Python-patterns – A collection of design patterns/idioms ... by TekMol on Tuesday May 31, 2022 @02:05PM (#31572772)
Wow!

So with this we can skip the class definition, having "self" as the first parameter of every function and prefixing all class variables with "self".

That leads to so much cleaner code!

Re:Python-patterns – A collection of design patterns/idioms ... by jwilk on Tuesday May 31, 2022 @01:14PM (#31572092)
What species of cat is that?
Re:Python-patterns – A collection of design patterns/idioms ... by lfx on Tuesday May 31, 2022 @07:49PM (#31576323)
Re:Python-patterns – A collection of design patterns/idioms ... by CraigJPerry on Tuesday May 31, 2022 @06:10PM (#31575540)
Re:Python-patterns – A collection of design patterns/idioms ... by aston on Tuesday May 31, 2022 @03:24AM (#31566941)
I don't think you want this, but just in case you do :)

    def encapsulate(mod):
        import types

        out = types.SimpleNamespace()

        def replace_global_scope(f):
            # via https://stackoverflow.com/a/1144561, but tweaked for py3
            return types.FunctionType(
                f.__code__,
                out.__dict__,
                f.__name__,
                f.__defaults__,
                f.__closure__,
            )

        for name in dir(mod):
            val = getattr(mod, name)
            if callable(val):
                val = replace_global_scope(val)
            setattr(out, name, val)
        return out
Re:Python-patterns – A collection of design patterns/idioms ... by Starlevel001 on Tuesday May 31, 2022 @05:38AM (#31567578)
This is essentially just a class but worse.
Re:Python-patterns – A collection of design patterns/idioms ... by TekMol on Tuesday May 31, 2022 @03:41AM (#31567013)
Re:Python-patterns – A collection of design patterns/idioms ... by abeyer on Tuesday May 31, 2022 @03:13PM (#31573612)
You moved the for loop inside the inner function, rather than outside.
Re:Python-patterns – A collection of design patterns/idioms ... by klyrs on Tuesday May 31, 2022 @11:45AM (#31570900)
I'm having trouble determining if this is parody. It appears that you want modules to replace classes, and also for users to roll their own __init__ methods? I guess that's "lean" in the sense of malnutrition, but I don't know about "nice."
Re:Python-patterns – A collection of design patterns/idioms ... by akx on Tuesday May 31, 2022 @02:22AM (#31566633)
You could absolutely write `encapsulate`, but you will lose out on all of the established code completion and other IDE features.
Re:Python-patterns – A collection of design patterns/idioms ... by TekMol on Tuesday May 31, 2022 @02:27AM (#31566651)
How would encapsulate look like? Would it have a negative performance impact?

I don't care about code completion at all. When I type "a", I don't want anything to happen on my screen except for an "a" to appear. I think that all the flickering stuff that the average programmer experiences on their screen burns more mental energy than it saves.

Re:Python-patterns – A collection of design patterns/idioms ... by LittlePeter on Tuesday May 31, 2022 @03:54AM (#31567073)
I once had a working "encapsulate" but lost the code. That code was modified version of this: https://docs.python.org/3/library/importlib.html#approximati...

My "encapsulate" was 4 lines of code at most IIRC. As someone mentioned, autocomplete in PyCharm did not work.

Re:Python-patterns – A collection of design patterns/idioms ... by _ZeD_ on Tuesday May 31, 2022 @07:46AM (#31568431)
I hope you are talking about code you use internally and don't share.

FWIW I care about code completion, and using a library that broke my workflow is not fun.

Re:Python-patterns – A collection of design patterns/idioms ... by tekknolagi on Tuesday May 31, 2022 @02:53AM (#31566771)
You could normally do this with copy.copy or copy.deepcopy but apparently you can't pickle a module object... A quick internet search does not immediately reveal anybody trying to do this before, and I am surprised.
Re:Python-patterns – A collection of design patterns/idioms ... by imurray on Tuesday May 31, 2022 @05:17AM (#31567465)
> apparently you can't pickle a module object

Not with the standard library pickle, but "dill extends python’s pickle module for serializing and de-serializing python objects to the majority of the built-in python types".

— https://pypi.org/project/dill/