#internals-and-peps

1 messages Β· Page 51 of 1

rich wharf
#
#Proposed
for x in [1, 2, 3], y in [4, 5, 6]: pass
#Current
for x, y in itertools.product([1, 2, 3], [4, 5, 6]): pass

#Proposed
for i in range(5) if i > 2: pass
#Current
for i in filter(lambda i: i > 2, range(5)): pass
for i in (i for i in range(5) if i > 2): pass

#Proposed
return 5 + 5 if 4 > 2
#Current
if 4 > 2:
  return 5 + 5
brave badger
#

I also think that for x in xs, y in ys is pretty nice, though having a zip equivalent should exist too

digital briar
#

oh cool! Sounds fun to play with the parser

rich wharf
#

@brave badger what would a zip equivalent be like

peak spoke
#

Well you shouldn't put multiple things on one line in the first place, can't say I'm a fan of if cond:\n return ... if it's simple but the inline if doesn't look much better

rich wharf
#

I'm a fan of return ... if cond over if cond: return ...

narrow kettle
#

i recently got mad at someone in review for having an inline if in c#

#

so ya no i dont like it here eitehr

brave badger
#

In Haskell there's three different ways to do list comps

-- regular list comprehension
a = [(x,y) | x <- [1..5], y <- [3..5]]
-- [(1,3),(1,4),(1,5),(2,3),(2,4) ...

-- zipped list comprehension
b = [(x,y) | (x,y) <- zip [1..5] [3..5]]
-- [(1,3),(2,4),(3,5)]

-- parallel list comprehension
c = [(x,y) | x <- [1..5] | y <- [3..5]]
-- [(1,3),(2,4),(3,5)]
rich wharf
#

zipped list comprehension is basically the same as python zipped list comprehension

digital briar
#

how about this for zip?

for x, y in i1, i2:
  print(x, y)
peak spoke
#

the if after the return just looks confusing to me because I expect a return to be just that - a return, with an expression on the right

rich wharf
#

@digital briar that's used for tuples

#

actually wait

#

@digital briar yep, that's already used in Python

#

for x, y in i1, i2 iterates over two lists of two items, i1 and i2

worldly venture
red solar
#

??

rich wharf
#

@worldly venture Did you make it work differently with tuples?

worldly venture
#

nope

#

I'm thinking about how to do that

rich wharf
#

alright

#

I have an idea

worldly venture
#

let me send code

rich wharf
#

doesn't Python AST have the col attribute

#

or something like this

worldly venture
#

ah yes

rich wharf
#

you should check that and see if it's greater than what it would be without the (

worldly venture
#

hmmm

brave badger
#

How should it look when separated to multiple lines?

rich wharf
#
for (
  foo in [1, 2, 3],
  baz in [3, 2, 1],
  xyz in ['x', 'y', 'z']
):
  print(foo, baz, xyz)
worldly venture
#

well

#

that's invalid syntax

rich wharf
#

unfortunately

worldly venture
#

but uh

#

lmfao

#
for foo in (
        [1, 2, 3],
        bar in [3, 2, 1],
        baz in [2, 4, 6],
        qux in [3, 6, 9]
    ):
    print(foo, bar, baz, qux)
#

that would work

brave badger
#

I mean, if the need to do that ever arises you're probably a few loops too deep already so it's kinda bad form

red solar
#

what's the goal here? i'm confused

rich wharf
#

to have fun with Python?

worldly venture
#

syntax sugar for nested loops @red solar

rich wharf
#

that too

red solar
#

oh

rich wharf
#

I want to make an extended Python with a lot of sugar

#

and more powerful lambdas

#

but I don't know how to edit the grammar for it to work

red solar
#

it literally just looks like you're getting rid of the repeated for keyword - nothing else is changing

#

is that really a worthwhile improvement?

rich wharf
#

Actually, a slight one

worldly venture
rich wharf
#

But the goal is just to have fun messing around with Python.

worldly venture
#

@red solar I'm just doing it for funsies

red solar
#

fair lol

#

wheres the code?

worldly venture
last pollen
#

exec(compile(source, "<ast shittery>", "exec")) epic

worldly venture
#

lol

brave badger
worldly venture
#

it works because python treats that source syntax as ```py
for foo in ([1, 2, 3], bar in [3, 2, 1], baz in [2, 4, 6], qux in [3, 6, 9]):
print(foo, bar, baz, qux)

rich wharf
#

@brave badger That's basically a switch case

worldly venture
#

interesting

brave badger
#

Isn't pattern matching more powerful than just a switch case?

last pollen
#

"switch case" as in the one most languages have is strictly for same type same length stuff innit?

#

pattern matching allows for more unreadability flexibility

narrow kettle
#

patternmathcing

#

ya

rich wharf
#

Guido replied

#

This might be added

#

Proposed*

#

They're proposing it right now

#

They're using case instead of as

#

and match instead of try match

worldly venture
#

yeah I prefer that

tacit hawk
#

is that nested foo shortcut a new feature?

brave badger
#

I think it'd also be nice if dataclasses can generate the dunders needed for pattern matching

shy vine
#

Only in Joethon

rich wharf
#

so basically

#
match result:
  case ONE: ...
  case TWO: ...
  case THREE: ...
  else: ...
#

something like this

#

is what they're proposing

worldly venture
#

@tacit hawk it is not, it's just exploiting python syntax to provide that

red solar
#

oh boy i hope they're not going to try and add match/case as a keyword

last pollen
#

!pep 3013

fallen slateBOT
#
PEP not found

PEP 3013 does not exist.

rich wharf
#

@red solar Guido is working on a proposal for it

last pollen
red solar
#

yeah i read the post

#

he knows what he's doing, but i can't imagine how he's gonna do it without a keyword

#

i saw the as

#

but it seems hmm

last pollen
#

he knows what he's doing, but i can't imagine how he's gonna do it without a keyword
overload more operators! lemon_swag

peak spoke
#

The new parser is the change that brought some propositions, a print statement was also mentioned somewhere a week or so ago

rich wharf
#

A print statement?

red solar
#

no way they switch print back to a statement lol

rich wharf
#

We can't be adding that back.

brave badger
#

Also calling unary functions without the parentheses

red solar
#

@last pollen what make unary => operator?

rich wharf
#

no, => is best for functions

peak spoke
rich wharf
#

x => 5 + x >>>>>>>>> lambda x: 5 + x

brave badger
#

Stuff like

len "hello"
ord "g"
last pollen
#

Also calling unary functions without the parentheses
that just reduces readability imo

rich wharf
#

yeah, I agree

last pollen
#

then again python mature consenting blabla

rich wharf
#

yield could become a function

last pollen
#

so it wouldn't really hurt to have that as an option

rich wharf
#

yield('5')

#

yield_from('4')

#

if unary functions are added

peak spoke
#

Haven't been following it but at least doubt that a parentheses-less calls will go through into an actual PEP

rich wharf
#

It's an equal level of horribleness

brave badger
#

Well I'm sure yield is a reserved keyword

spark magnet
#

they aren't talking about paren-less calls any more

last pollen
#

they aren't talking about paren-less calls any more
readability concerns? or just something not needed in the language?

spark magnet
#

i didn't follow it closely, but it seemed like kind of a non-starter

last pollen
#

guess I'll add that thread to my reading list

worldly venture
#

Oh yeah that got shot down quite hard

peak spoke
#

I don't see the need for a print statement but I'm sure there's more thoughtful discussion about that in the mailing list

tacit hawk
#

call without parentheses confuses with value access, no?

raven ridge
#

I don't see the need for a print statement but I'm sure there's more thoughtful discussion about that in the mailing list
Not really, most people said exactly that.

deft pagoda
#

that was really just the same person posting under different accounts, everyone else loved the idea

unkempt rock
#

lets go the ruby way

#

and make parens optional for single argument functions

#

:D

fossil jetty
#

Is there a way to get this functionality?

class Parent:
    def method(self, start=None, end=None, frq=None):
        if start is None:
            start = self.start
        if end is None:
            end = self.end
        if frq is None:
            frq = self.frq
            
        do_stuff()
        
class Child(Parent):
    def method(self, start=None, end=None, frq=None, new_param=None):
        # if start is None:                 <-- Get this for free?
        #     start = self.start
        # if end is None:
        #     end = self.end
        # if frq is None:
        #     frq = self.frq
        if new_param is None:
              new_param = self.new_param
unkempt rock
#

wow, free, greedy are we

#

also

fossil jetty
#

Does super run all of the code in the Parent class, then the code in the child class?

#

and would those local variables in the parent class end up in the child class? i.e. would the local variables (start, end, frq):

        if start is None:
            start = self.start
        if end is None:
            end = self.end
        if frq is None:
            frq = self.frq

be set in the child class?

unkempt rock
#

super().__init__() runs parent init code

spark magnet
#

@fossil jetty the attributes (not variables) are in the object (not the class)

raven ridge
#

super delegates to the next class in the MRO, which isn't necessarily a parent class of your class.

fossil jetty
#

here is a very minimal example of my problem:

from dataclasses import dataclass

@dataclass
class Parent:
    start: int = 1
    end: int = 2
    frq: int = 3
    
    def method(self, start=None, end=None, frq=None):
        if start is None:
            start = self.start
        if end is None:
            end = self.end
        if frq is None:
            frq = self.frq
            
        return sum([start, end, frq])
        
class Child(Parent):
    def __init__(self):
        super().__init__()
    
    def method(self, start=None, end=None, frq=None):
        super().method(start=start, end=end, frq=frq)
        return sum([start, end, frq])
#

My expectation would be Child does the same thing as Parent (assume MRO is just a line up)

#

I just want to move this:

        if start is None:
            start = self.start
        if end is None:
            end = self.end
        if frq is None:
            frq = self.frq

into the child class without having to rewrite it

raven ridge
charred wagon
#

Do you all think it would make sense for Python to have a list "view" feature? Kind of like slicing, except all the references aren't copied.

last pollen
#

what would be a usecase for that?

paper echo
#

Modifying a sub-list

#

Yeah seems niche

#

I just wish numpy and pandas had more explicit semantics around views

charred wagon
#

Wouldn't there be a performance benefit?

paper echo
#

I dont need it in base python

#

How often do you need to modify a sublist

charred wagon
#

What if you just need to iterate one?

paper echo
#

Without numpy style indexing

#

Wdym iterate one

charred wagon
#

for item in mylist[5:]:

#

But that creates a copy

paper echo
#

Item isnt copied though

charred wagon
#

But the reference still is

red solar
#

the reference is a pointer

#

copying it is basically free

#

(sure incref and whatnot but still)

charred wagon
#

It copies the entire list though

red solar
#

if you had a view you would still need to incref

charred wagon
#

Well, everything past 5

paper echo
#

What would you want? to be able to assign to item and modify the value thats stored in the list?

#

Seems like a valid performance optimization for cpython I guess

charred wagon
#

It's just a performance thing, to save memory

paper echo
#

Theoretically you could write a C extension listview() or something

red solar
#

i'm confused as to how a list view would work - you have a reference to said list? that's it?

paper echo
#

Or maybe not

red solar
#

wouldn't it still call the original list's iter method through the view?

paper echo
#

@red solar like slicing a numpy array but theres no guarantee even there that you arent copying pointers

red solar
#

you can't just write your own view?

#

composed of a sequence, an offset, and a size?

charred wagon
#

Maybe but the point of my question is if it should be a standard Python feature

red solar
#

oh

#
class SequenceView:
    
    __slots__ = ("_seq", "_offset", "_step", "_size")

    def __init__(self, seq: MutableSequence, offset: int = 0, step: int = 0, size: Optional[int] = None):
        self._seq = seq
        self._offset = offset
        self._step = step
        self._size = size
#

@charred wagon something like this?

charred wagon
#

Yes, I suppose that would work just for iteration. I think being able to mutate it would be cool but it's a more niche use case.

red solar
#

you can mutate this, this was more a question of what information it would carry

charred wagon
#

Yeah basically like a slice, but it's a view

deft pagoda
#

i've wanted listviews before, and still do

#

though islice can sort of do most of things i want it for

charred wagon
#

I could live without a step though. May make the implementation simpler too

#

The problem with islice is that it will iterate to skip stuff

red solar
#

should probably default to 1 πŸ˜‚

deft pagoda
#

yep

charred wagon
#

So if you start at 1 million it will iterate 1 million items

#

😦

deft pagoda
#

thats the worst part of it, but its a high performance skip

red solar
#

islice doesn't work on indexable types?

charred wagon
#

It treats it as an iterable

red solar
#

like list.index is O(1)

#

oh

deft pagoda
#

might be able to do something with stride_tricks

charred wagon
#

What's that?

last pollen
#

could be that

red solar
#

this is so much simpler in c/c++, you just have a pointer and a size πŸ˜‚

deft pagoda
#

stride_tricks isn't really well-documented, but it moves through memory

#

seems like you could use it to make a view

charred wagon
#

I'm pretty it would have to be implemented as a C extension

#

Or make use of something that is a C extension

#

Which yeah, would in turn work with the pointer as you say

#

Maybe it's not so trivial, what do I know

red solar
#

what was wrong with my earlier idea? it works with any sequence type

deft pagoda
#

point was to save memory, not create more overhead

red solar
#

less memory overhead, less computational overhead, pick one

deft pagoda
#

both, views provide both

#

it's not an exclusive or

red solar
#

views are a pointer and a size - in python that's PyObject * self._seq and PyObject * self._size

#

ohh

#

i see

#

the pointer can point to any element, whereas we need an offset to do that :/

#

but still it's like 1 extra pointer, and negligeable overhead

deft pagoda
#

with stride_tricks, you could point to the start location and then just move along the array by the step size

red solar
#

so you could only iterate through it once?

#

that's not really a view

#

it's a single use iterable

deft pagoda
#

you could iterate through it multiple times

#

it's not a generator

red solar
#

i'm confused, what information does stride_tricks store?

deft pagoda
red solar
#

a pointer to the start location, and the step size? nothing else?

cloud crypt
#

opinions on private attributes/functions in python?

#

I think it would be a fine thing to have to be honest

radiant fulcrum
#

😩 Constants would be nice for debugging every once and a while

brave badger
#

The protected convention with an underscore is fine enough as it is to be honest

glass robin
#

constants are usually written capitalized

radiant fulcrum
#

yeah

#

but thats all well and good until it starts changing and theres 15k lines of code that could be doing it

#

xD

#

saying that i think the issue i got when that happened was a race condition rather than stuff being overrode

brave badger
#

Do linters pick up constants being modified?

radiant fulcrum
#

dont think so

#

Pycharm didnt atleast

#

but it was a pretty big slew of code with the const being used by alot of things

timid orbit
#

what's a good way to sync up some code to a precise timer?

glass robin
#

perf_counter?

#

only works for relative time

timid orbit
#

let's say I have something running on main thread that I want to loop every 1/120th of a second, but nothing more and nothing less, while the function itself takes a variable amount of time to run

#

would it be better to have an inner loop that waits for the time to be over, or instead should I use a second thread that unlocks and relocks a lock every 1/120th of a second

hazy anchor
#

That sounds like an incredibly tall order to me.

raven ridge
#

The operating system's scheduler will not wake you up with sub-millisecond accuracy. And there is no guarantee that your thread will be scheduled, and won't be preempted, during that sub-millisecond window. If you need this to predictably run exactly 120 times per second, I promise that you will not get that working without an RTOS.

#

Or a microcontroller or peripheral dedicated to doing only this job.

raven ridge
#

Or by writing it as a kernel module / device driver, perhaps.

hazy anchor
#

Although i have a proven algorithm. Each loop if it hasn't been exactly 120ms, skip and try again

hollow crane
#

Yeah I imagine the average call frequency is more important than gap between calls

signal bear
#

i am not sure what cloud service to use

#

I have a dashboard, and I'm looking at the free tier on AWS

#

but I'm not sure which "type" to use

#

idk if i need aws glue

glass robin
signal bear
#

thx

#

on a side note, looks like python has a crontab module

#

I am curious if i can run a certain function from a .py file using the module, or if it would default to running the whole script

#

i was considering using crontab to schedule an extraction of data from a github page

#

basically im just trying to update the dataset im using, I don't know if it would make sense to run the whole script again, or if I should just try to run the function that extracts the data

hazy anchor
#

probably one for the python general channel, but in short adding a command line flag to the script that runs just the refresh part would be what i'd do.

ocean igloo
#

Hmm guys i think i need a help or somr guide or the way to understand docs more, i am more comfortable with yt tutorials but i think reading docs is better, what is good way to move from yt tuts to docs?

#

Especially real devs dont have time probably to watch 3h of yt content that thry can check in docs

uncut sage
#

@ocean igloo Perhaps pick a module or project you're already familiar with, and look at its documentation to see how the ideas in it are communicated in the docs

#

or, pick a module or project you'd like to work with starting from scratch, and give the docs a slow once-over. A project that has examples or a tutorial is a good place to do this with (e.g., Django, which is big and has tons of moving parts, but has a fantastically detailed tutorial and excellent docs).

#

But your instinct to transition to reading docs is a good one, because teaching yourself how to parse project docs is a powerful skill.

gloomy rain
ocean igloo
#

Oh okay i am sorry

#

Tq serdar i ll try it

boreal umbra
#

Did anyone see the proposal about a try match block? I don't understand what functionality it's adding.

boreal umbra
#

But how is it better than if-elif-else?

uncut sage
#

Presumably there's some kind of hashing operation so that the match can be done that way instead of needing to traverse the entire chain of matches, but I'd have to see what GvR is proposing in detail.

boreal umbra
#

I see

open trout
#

wait is it a case switch PEP?

uncut sage
#

To my mind that would be the only real advantage: have some kind of dict-under-the-hood-style matching going on, because otherwise you just have syntactic sugar for if/elif/else

boreal umbra
#

It looks similar to case switch but the PEP isn't up yet

open trout
#

ah

#

sounds fun, I like having syntactic sugar

boreal umbra
#

I like having syntactic sugar except for the name of that concept.

#

I also hate "pythonista"

open trout
#

lmao

#

you mean you don't like syntactic sugar or you don't like the naming of try match?

boreal umbra
#

I don't like the phrase "syntactic sugar". Something about it rubs me the wrong way

open trout
#

right

boreal umbra
#

but I think trying to extract more use cases out of existing keywords is fun.

open trout
#

I misspelled it as "synthetic sugar" and "syntax sugar" for a looong time

boreal umbra
#

we could just call it... "nice syntax"

#

or "pythonic syntax"

open trout
#

the first one sounds a little too everyday

worldly hedge
#

syntactic sugar is perfect

open trout
#

the latter is unnecessarily specific

worldly hedge
#

Sugar only makes things taste/feel better

#

that is what syntactic sugar does

#

e.g. you could write promises which are ugly asf on js, but now, you can use async/await

#

so much nicer

open trout
#

Imagine there used to be a time when f-strings didn't exist

#

I've grown so used to them now

#

pre-3.6 was dark times

worldly hedge
#

man speaking out facts right now

open trout
#

only 90's kids will remember %-formatting

boreal umbra
#

% formatting made me so sad

#

but I guess it's better than unreadable chains of + signs

open trout
#

oh yikes year

#

yeah

radiant fulcrum
#

tbf i still use .format alot

#

pypy especially seems to have moments where it goes holy fuck what are these weird f thing

open trout
#

lmao

#

"I have never seen this man in my life"

#

new venv who dis

sacred tinsel
#

@boreal umbra I'm not sure what the proposal looks or will look like, but a switch-case does not necessarily behave like an if-elif-else since all cases below the triggered one are executed (I think there are languages where that isn't the case, but it is in C and Java)
For example, pseudocode:

switch (x)
  case "A":
    print("A")
  case "B":
    print("B")

if x == "A" then we print both "A" and "B", a break is used to stop the fallthrough (often you see each case end with a break)

#

there are some very attractive optimization possibilities which I'm generally not smart enough to understand but they likely wouldn't apply to Python

boreal umbra
#

Ah I see

open trout
#

it's kinda uncanny valley seeing that code

#

weird feeling

boreal umbra
#

I don't know that I like the implicit fallthrough though

void sonnet
#

Did anyone see the proposal about a try match block? I don't understand what functionality it's adding.
[....]
It looks similar to case switch but the PEP isn't up yet
@boreal umbra that was my take as well. it read much like a switch. it was also funny that GVR had already worked on it as well with the new parser.

open trout
#

looking forward to reading the PEP

#

good times

flat gazelle
#

I hope it will go more the route of ML pattern matching than C imperative switches

void sonnet
#

wdym? (not familiar with ML pattern terminology)

#

like using approximation and probability? throwing darts... πŸ˜„

flat gazelle
#
import constants

match config:
    case {"route": route}:
        process_route(route)
    case {constants.DEFAULT_PORT: sub_config, **rest}:
        process_config(sub_config, rest)
``` something like this (from the new PEP that just popped up)
void sonnet
#

ahh. didn't see the PEP. nice!

tawny shoal
#

Is __init_subclass__ called before or after the metaclass' __new__? Are they just incompatible?

worldly venture
#

Hey there's the PEP!

boreal umbra
#

really? what number?

paper echo
#

oh boy

#

Protocols + pattern matching = heaven?

flat gazelle
#

protocols?

paper echo
#

oh god we are going to get so many "why doesn't case work" questions from people who know other programming langauges

#

!d g typing.Protocol

fallen slateBOT
#
class typing.Protocol(Generic)```
Base class for protocol classes. Protocol classes are defined like this:

```py
class Proto(Protocol):
    def meth(self) -> int:
        ...
```  Such classes are primarily used with static type checkers that recognize structural subtyping (static duck-typing), for example:

```py
class C:
    def meth(self) -> int:
        return 0

def func(x: Proto) -> int:
    return x.meth()

func(C())  # Passes static type check
```  See [**PEP 544**](https://www.python.org/dev/peps/pep-0544) for details. Protocol classes decorated with [`runtime_checkable()`](#typing.runtime_checkable "typing.runtime_checkable") (described later) act as simple-minded runtime protocols that check only the presence of given attributes, ignoring their type signatures.

Protocol classes can be generic, for example:

```py
class GenProto(Protocol[T]):
    def meth(self) -> T:
        ...
```   New in version 3.8.
paper echo
#

@flat gazelle ^

#

"structural subtyping"

flat gazelle
#

ah, neat

paper echo
#

i really wish we had better support for generics though

#

as far as i know, you can't currently define types like A[B] in a way that mypy can understand

#

like if you wanted to define a tree where the data contained only ints, you can't write a Tree class such that Tree[int] does what you'd want it to do

#

(at least, that i know of)

flat gazelle
#

I wonder if we will ever get something like flow (JS inferring, partially dependently typed, type checker)

#

you maybe can with typing.conditional and make the check yourself or sth. But it does kind of defeat the point

paper echo
#

typing.conditional?

weak lotus
#

help i am having errors

flat gazelle
#

ah, nvm, I am misremembering

paper echo
#

what i would really like is for mypy to be able to identify that

class MyRecord(TypedDict):
    name: str
    age: int

is a subclass of

Dict[str, Union[str, int]]
#

and booo generics are excluded from pattern matching

#

so much for this:

case List[str]:
#

i can see why but still

cloud crypt
#

okay I just went through PEP 622

#

and it's really awesome πŸ‘

paper echo
#

will this work with something like

case None:
    ...
case .math.nan:
   ...

? seems like no because it uses == internally for literals

north root
#

match statement would be quite interesting, but i'm still waiting for a none-aware operator

flat gazelle
#

that would be nice

north root
#

probably won't happen though

flat gazelle
#

dict merge made it in, I think this is about the same

north root
#

!pep 505

fallen slateBOT
#
**PEP 505 - None-aware operators**
Status

Deferred

Python-Version

3.8

Created

18-Sep-2015

Type

Standards Track

north root
#

deferred unfortunately

paper echo
#

dict merge ++

#

the maybe-. and maybe-[ are clever

#

i wonder if this has value though

#

the walrus operator already covers a lot of use cases

#

and we don't want to end up like raku or haskell

#

operator salad

shy vine
#

Not something we're going to help with on this server

cloud crypt
#

solving image recaptcha is not quite possible anyway

visual shadow
#

Heck, humans fail recaptcha πŸ˜…

cloud crypt
#

voice captcha solving is somewhat feasible

minor sinew
#

voice sounds easier anyways

#

fourier transform ftw

cloud crypt
#

there are some nice attempts on it

#

what does pure python mean in this context if you will need AI

shy vine
#

It doesn't matter. We're not assisting.

raven ridge
#

and even if we were, it would be off topic for this channel πŸ˜„

#

I dislike that PEP 622 proposes making _ not bind a variable. I get why they're doing it, but it feels inconsistent to make it special like that... πŸ˜•

red solar
#

inconsistent how?

#

isn't it fairly common to use _ as a placeholder?

#

i think go even enforces it

raven ridge
#

it's common in Python today, but when you do it creates a real variable named _

red solar
#

but the whole point is that you never use it

raven ridge
#

that is, it's convention that makes it a placeholder, not language semantics. This would be inconsistent with other uses of _ in the language today.

red solar
#

oh wait this is python

#

you can do _, _ = (a, b)?

raven ridge
#

!e

_, _ = 1, 2
fallen slateBOT
#

@raven ridge :warning: Your eval job has completed with return code 0.

[No output]
raven ridge
#

yep.

red solar
#

oh then what's the point of pep 622?

#

optimisation?

charred wagon
#

Other languages have done it because it reduces memory allocation I believe

#

Couldn't Python benefit from that too?

red solar
#

not sure it would benefit in any significant way

raven ridge
#

saving what memory?

red solar
#

since rn _ would just be PyIncref(obj); PyObject * new_obj = obj;

raven ridge
#

the usual benefit of a switch statement in a C-like language is that the cases aren't evaluated linearly. This proposal won't have that benefit.

red solar
#

are we talking about _ or switch?

raven ridge
red solar
#

oh it does both

#

why tf does pattern matching need _ to be reserved?

raven ridge
#

[*_] matches a sequence of any length.
I think it needs something to be reserved in order to make that possible.

paper echo
#

because you need a wildcard

#

unless they use typing.Any instead

red solar
#

they couldn't introduce a new singleton type?

raven ridge
#

* could have been the wildcard, and would have been a better one IMHO, but that won't work for a wildcard sequence of any length.

paper echo
#

Any is literally what they want

#

i agree that elevating _-as-placeholder from convention to language spec is unsettling

red solar
#

is the pep still open to changes?

raven ridge
#

the PEP could also use some reworking to make sure that the examples don't use syntax before the section explaining that syntax, heh

#

yeah, this is just a proposal at this point.

paper echo
#

yeah im not a fan of the . either

#

privileging literals too

#

makes me squirm

open trout
#

lmao

#

.BLACK

#

villager hmm sound

unkempt rock
#

I do python now

cloud crypt
#

we all do python here

open trout
#

maybe the real python was the friends we made along the way

raven ridge
#

To match a sequence pattern the target must be an instance of collections.abc.Sequence, and it cannot be any kind of string (str, bytes, bytearray).
That's weird too - this would be the only context I can think of where a string cannot be unpacked exactly like a list...

red solar
#

what about memoryview? does that count as a string?

paper echo
#

@raven ridge yeah i dont like that either

#

id rather add a collections.abc type that's a "Sequence but not a String"

red solar
#

can it match List[str]?

raven ridge
#

of course; nothing in the PEP says that it couldn't.

red solar
#

so what's the issue with str? you can essentially treat it like List[str]

raven ridge
#

the issue is what I said - in a normal Python context, you can do:

a, b, c = "abc"
print(a, b, c)
``` but in this proposal, you would not be able to do ```python
match "abc":
    case a, b, c:
        print(a, b, c)
``` even though you could do ```python
match ["a", "b", "c"]:
    case a, b, c:
        print(a, b, c)
``` so it's inconsistent with other ways that a string can be unpacked.
molten onyx
#

This has a few strange limitations that I hope they figure out but I really like this overall

red solar
#

but i don't see why that has to be a limitation?

molten onyx
#

I'm sure the mailing list's got an answer somewhere pithink

honest narwhal
#

Python is getting case statement things?

red solar
#

maybe

honest narwhal
#

I saw it in another server

#

but I don't understand what they meant

raven ridge
molten onyx
#

Guido the madlad actually did it

paper echo
#

the only reason i can see Sequence not containing Str is for pandas/numpy compat

#

which i think is... not right.

#

to privilege in the core language

raven ridge
#

they don't seem to explain the rationale, either - that's the only instance of the word bytearray in the entire PEP, and bytes appears in only one other spot. πŸ˜•

shy vine
#

!shhh

fallen slateBOT
#

βœ… silenced current channel for 6 minute(s).

shy vine
#

!ban 724989356221399060 Take your terrible memes somewhere else

fallen slateBOT
#

:incoming_envelope: :ok_hand: applied ban to @wise delta permanently.

shy vine
#

!unsilence

fallen slateBOT
#

βœ… unsilenced current channel.

cloud crypt
#

yikes

red solar
#

thought ela got mad at the string issue for a sec πŸ˜‚

tacit hawk
#

is not Python introspection slow? that pattern matching will use a lot of it, no?

red solar
#

also i may have misunderstood how this pep works

#

so imma go read it in full now

#

i don't think it should be much slower than just if/elif/else?

#
match number:
    case 0:
        print("Nothing")
    case 1:
        print("Just one")
    case 2:
        print("A couple")
    case -1:
        print("One less than nothing")
    case 1-1j:
        print("Good luck with that...")
#

like literal matching should be fast

paper echo
#

afaict it literally translates to if/elif/else

#

so match x: case int: is if isinstance(x, int):

tacit hawk
#

Yes I saw, its not too complicated to the interpreter

subtle pike
#

This looks highly useful

tacit hawk
#

some instance type checking and direct attribute access

subtle pike
#

It's still a decent QoL improvement

cloud crypt
#

@paper echo are you sure it works like that

#

with the types

#

saw match x: case int():

tacit hawk
#

yes that will make the code more clean

subtle pike
#

I agree with the above opinion that the prefixed dot operator to enforce name resolution is a bit odd

#

More syntax to remember lemon_sentimental

paper echo
#

yeah i dont like it one bit

#

its completely out of left field from existing python syntax

raven ridge
#

I'd have gone the other way, I think... Rather than making name capturing be the default and adding special syntax for accessing variables, I'd have made accessing variables the default and added special syntax for capturing...

paper echo
#

^^^^^^^^^^^

subtle pike
#

They could've done something interesting with a new operator like & to bind expressions instead of patching on the dot operator to do name resolution

#

I'd prefer a completely distinct operator for readability

#

though

#

I can see the rationale for the dot operator since it's being treated as a resolution in the current namespace

#

Meh, I guess I can only be averse to the semantics not the syntax

#

Syntax is bikeshedding

paper echo
#

its not bikeshedding if the entire project is designing a bike shed

cloud crypt
#

lol

paper echo
#

the dot syntax is the programming equivalent of a norman door

raven ridge
#

This is the time to have opinions on the syntax; that's the entire purpose of a PEP.

weak lotus
#

Someone willing to help me fix a small problem on big project pm me

subtle pike
#

I'm trying to say that the dot operator as the operator to specify name res isn't as important as the fact it's doing name res to begin with

cloud crypt
#

we don’t do dm-help and you’d better go to one of the help channels @weak lotus

subtle pike
#

I can see where they were going for but it feels incredibly inconsistent with the rest of python

paper echo
#

right. as godlygeek said

subtle pike
#

If there was a more fleshed out resolution operator then I'd be more relaxed

#

But this is ad-hoc

cloud crypt
#

It needs a lot of reconsideration imo

#

current ideas are ambitious but not quite pythonic

subtle pike
#

It's an interesting cross between guards and scheme's case analysis

raven ridge
#

I like the idea of making unary & or + do the name binding, so that a name without that unary operator applied to it unambiguously means a variable resolution.

subtle pike
#

I'd stick to & since that's an easy map to a reference in other languages

#

Immediately recognisable as you're indirectly mutating that value if you're familiar with other languages with pointers

raven ridge
#

it's actually used as a unary operator in Cython, though, so it would be somewhat ambiguous there...

subtle pike
#

ah :/

#

perhaps they could retrofit @?

#

it's still a hack though

raven ridge
#

that wouldn't be so bad, I think - it's currently a binary operator, it could be made unary as well, and you could read "@x" as "match and store at x"

subtle pike
#

You know

#

They could introduce a brand new operator entirely

#

$ as a match and bind

raven ridge
#

There's usually more aversion to introducing a new operator than to repurposing an existing one.

weak lotus
#

anyone down to hop in a vc and help me fix an exception problem

raven ridge
#

I guess using an operator to indicate binding wouldn't play so nicely with class patterns, though. Instead of python match shape: case Point(x, y): ... case Rectangle(x0, y0, x1, y1, painted=True): ... you'd need python match shape: case Point(@x, @y): ... case Rectangle(@x0, @y0, @x1, @y1, painted=True): ...

subtle pike
#

After the walrus operator debacle, that's certainly evident. However, this is an entirely new behaviour that's hard to generalise to other operators

#

the walrus operator was a visual change to regular assignment which gave the reader a hint

#

retrofitting another operator may end up with more confusion because now you have two entirely different behaviours

flat gazelle
#

personally, 2 different things doing 2 different things is better than 1 thing doing 2 different things depending on context

raven ridge
#

would it be weird to see the new operator on the RHS of an = for a keyword argument being bound? ```
case Rectangle($x0, $x1, $y0, $y1, painted=$is_painted)

tacit hawk
#

Yes, better keep with the dot

subtle pike
#

bare in mind this is a discussion where the $ operator is for binding, not name resolution

flat gazelle
#

I would be ok with

case {BLACK}
#

it matches fstrings

subtle pike
#

true

flat gazelle
#

but conflicts with sets

subtle pike
#

then again, Guido hates brackets yert

flat gazelle
#

though it should not matter, because you cannot unpack into a set, so I would guess you cannot match on it either

#
In [9]: {a} = [1]
  File "<ipython-input-9-24859e2b15c3>", line 1
    {a} = [1]
    ^
SyntaxError: cannot assign to set display
subtle pike
#

I think that's too ambiguous

#

It makes sense when you think about it but I can still see myself slipping up regularly

paper echo
#

please no operator salad

#

sincerely,
salt

#

raku is raku, python is python

#

?? and ?. and := are fine, they serve a serious need

flat gazelle
#

you mean <<+<< is not the perfect operator

raven ridge
#

if we need an operator to disambiguate referring to a name and capturing a name, I'd much rather see it on the capture (the new concept) instead of the reference (the existing concept)

subtle pike
#

Can't wait for the "and-then" pipe operator

#

It'll truly ascend as a language

#

/s

flat gazelle
#

can't wait until python adopts rakus custom operator system. def postcircumfix:< < > >(a, b):

raven ridge
#

looks like C++ πŸ˜„

raven ridge
#

int(i) matches any int and binds it to the name i.
blink

#

why make that the default behavior, instead of having people do i := int() ? That seems really strange to me...

raven pike
#

hey guys

#

is this kind of "choice"?

raven ridge
red solar
#

wait what was wrong with just having default like every other language?

#

i mean if they're adding keywords...

#

int(i) matches any int and binds it to the name i.
@raven ridge tf

flat gazelle
#

most languages with pattern matching use either _ or otherwise, which is just alias for True, or else

raven ridge
#

the argument there would be that they're already adding a wildcard, and that it makes sense to use the wildcard alone to indicate "anything else"

red solar
#

ah hmm

flat gazelle
#

default is more common with imperative switches.

raven ridge
#

otherwise you're adding two different types of wildcards, one that's only usable at the top level and one that's usable inside a submatch.

cloud crypt
#

default sounds like a way to do it tbh

raven ridge
#

would you get rid of _ then?

cloud crypt
#

where?

#

ah

#

I see

raven ridge
#

if there's a two-tuple and you want to get just the first value from it, this lets you use case (x, _) as the pattern.

flat gazelle
#

having just spent the whole day using _ as wildcard in elm pattern matching, it works well

paper echo
#

@raven ridge yeah i am not a fan at all of that, and im actually surprised this pep is being accepted with that stuff in it

#

the int(i) binding

#

i know its like how functional langs do it

#

but

raven ridge
#

this PEP isn't being accepted at all yet; it's just a proposal so far.

paper echo
#

ah ok i thought it was further along

#

the problem here is that namespacing is hard

#

i get why they did the . thing

raven ridge
#

the PEP is starting the discussion, not ending it. it was just published today.

paper echo
#

its just so weird

#

cool

#

you know what id settle for?

#

typing.Literal[BLACK]

#

with the extension that typing.Literal[BLACK, RED] is equal to typing.Unionp[typing.Literal[BLACK], typing.Literal[RED]]

#
v1 = 1
v2 = 2

match number:
    case Literal[v1]:
#

but maybe that can get conflated with the destructuring

#

we GNU R now boys

red solar
#

oh no 😦

flat gazelle
#

honestly, I still like

v1 = 1
v2 = 2

match number:
    case {v1}:
paper echo
#

im trying to think of a heinously convoluted R example off the top of my head but i cant rn

#

@flat gazelle how does that work with sets being a valid collection type?

raven ridge
#

I'm still working my way through this PEP - there's a lot to unpack here (pun intended). But one thing that strikes me is that it will be backwards incompatible for a class that doesn't have a __match_args__ to add one in the future, because it will change the meaning of Class(pattern) in existing case expressions...

paper echo
#

that too

#

lots of onus on library devs

#

as if adding mypy stubs wasnt enough

flat gazelle
#

are they? They are not valid targets for unpacking, but I guess they do make sense to be matchable. Did not think about that

paper echo
#

i still cant believe all this is to avoid if/else

cloud crypt
#

if I get it right, problem is to decide whether given x in case x should update x or be checked against x?

paper echo
#

yep

#

this is literally the problem faced by R's nonstandard evaluation

cloud crypt
#

case x to assign x sounds pretty dumb

paper echo
#

i agree. but what if it's parameterized

#

"i want to match such that the first element is equal to "ZZ" and the 2nd element is captured"

#

how do you express that?

cloud crypt
#

ah fair

paper echo
#

for that matter, "i want to match such that the first element is equal to "ZZ" and both elements are captured"

cloud crypt
#

.something makes sense at this point

paper echo
#

because == can be arbitrarily redefined by any object at any time

cloud crypt
#

short and fine once you get it

paper echo
#

yeah it just

#

literally looks like R

cloud crypt
#

lol

paper echo
#

and i love R

raven ridge
#

i still cant believe all this is to avoid if/else
@paper echo It's not to avoid if/else, since (unlike a switch in most languages), the performance characteristics are exactly the same as if/else. It's to provide pattern matching on arbitrarily complex subexpressions.

cloud crypt
#

well

paper echo
#

@raven ridge but how often is that desirable

#

compared to allowing isinstance to work on Protocol subclasses

#

we should be moving away from unqualified tuples and dicts

#

we have friggin dataclasses now

raven ridge
#

it works on classes, as well, not just unqualified tuples and dicts. this would be quite useful on dataclasses.

cloud crypt
#

is there some smart isinstance that supports typing?

#

just wondering

paper echo
#

not yet, no

#

at least nothing that supports generics

cloud crypt
#

did no one really write one

#

bruj

paper echo
#

because at runtime you'd have to check every element of an iterable to determine that it's an Iterable[int]

#

which is obviously problematic if said iterable is infinite

#

it's not a matter of will or manpower

cloud crypt
#

I see

paper echo
#

this is why im saying extend isinstance to Protocol

#

or even dataclasses for that matter this makes no sense they already work

cloud crypt
#

gotcha

paper echo
#

yeah i just dont see how often you really need this kind of aggressive destructuring matching

#

its replacing "lines of code" with "syntax magic"

#

in a way that doesnt seem helpful

#

again, contrasting with the None-aware operators which have clear semantics and significant complexity reduction benefit

#

i would sooner adopt something like glom or toolz.get_in before this pattern matching

#

/rant

raven ridge
#

On a very high level it is similar to regular expressions, but instead of matching strings, it will be possible to match arbitrary Python objects.
I think that's a good framework to view the proposal in.

#

I think I would have wanted this more than never, but not terribly often.

#

the guards are probably the most useful part of it relative to plain if/else blocks, I think

#

at least, in terms of where it would be most useful in an average Python program...

#

because that gives you the ability to fall down into another case block even if you already partially matched in one, which tends to be a place where if / elif gets messy.

paper echo
#

but then couldn't you make match/case just act syntactically like if/elif/else

#
match <expr>:
case <expr>:
case <expr>:
#

so its just if/elif with fallthrough

#

or hell give me a special keyword to manually fall through

raven ridge
#

there was an alternative proposal I heard kicking around, which may eventually see light as a different PEP, to augment existing unpacking methods to support a kind of structured unpacking of dictionaries and classes, instead. {"outer": {"inner": value}} = some_dict; print(value) or the like.

chrome oak
#

elif

subtle pike
#

For what it's worth, I would've preferred else as a catch-all statement like scheme

#

It'd be consistent with else's appearance everywhere in pythons compound statements

raven ridge
#

but my point above stands - as long as _ is a catch-all wildcard in a submatch, it would be inconsistent to not allow it as a catch-all wildcard in a top-level match, and if you already have that as a catch-all wildcard, adding a second one would just be syntactic sugar.

#

that is, as long as case (x, _) works, case _ should work as well, and as long as case _ works, there's no reason to add an else that's semantically equivalent to case _

subtle pike
#

I suppose

paper echo
#

@raven ridge I like that version better, because it's an extension of existing syntax rather than something completely new that also forces a bunch of really difficult decisions like the ones we are discussing now

supple heath
#

Someone knows how Can I automate some SSH tasks using python? (Usually I open an SSH terminal, go to some paths, execute some scripts and get some informations on the screen to excel.. etc) Could I do it using python? Someone knows what I need to learn to do that?

latent marten
#

Someone knows how Can I automate some SSH tasks using python? (Usually I open an SSH terminal, go to some paths, execute some scripts and get some informations on the screen to excel.. etc) Could I do it using python? Someone knows what I need to learn to do that?
@supple heath https://medium.com/@simon.hawe/save-time-by-automating-ssh-and-scp-tasks-with-python-e149de606c7b and I think that for working with Excel you'd want to use openpyxl

Medium

I saved a lot of time automating ssh & scp tasks using python. This story shows you how I did that.

supple heath
#

thanks

unkempt rock
#

An interactive python shell can also be handy, since you can add functions on the fly and stuff

#

But maybe you really want an orchestration tool for this task.

#

Well, there are orchestration tools written in Python as well.

supple heath
#

I really need to do a place.. (language - Python maybe) to do a lot of things.. Automate my repeated tasks.. Execute scripts, read screen, open Jira tickets, open excel, send e-mail.. etc

#

but I'm looking about Ansible or Python.. Idk what is better and what can I do

#

I know that I could do all this with python..

#

but someone told me about Ansible

void sonnet
supple heath
#

ohh.. sorry @void sonnet

void sonnet
#

hehe. no worries.

red solar
#

@raven ridge what version do u have as python3?

raven ridge
#

that definitely belongs in an off-topic channel. this machine is running Ubuntu 18.04, so 3.6.

red solar
#

Trust me it’ll be on topic

#

When you’re free can you try and write a c lib that uses _PyLong_FromByteArray? (or As)

#

As shown here

#

I can’t get CLion to pick it up on my Mac, even tho Python.h includes that header file

#

It’s not documented, but I don’t get why it’s not available, since PyAPI_FUNC should export it, and it’s definitely in the header file

#

@raven ridge

raven ridge
#

seems to work for me. I just quickly did:```c
#define PY_SSIZE_T_CLEAN
#include <Python.h>

int main()
{
void as_byte_array = (void)&_PyLong_AsByteArray;
return 0;
}

red solar
#

Oh hmm

#

Ok maybe I just need to update the documentation and add a macro so it doesn’t have an underscore then

raven ridge
#

I believe having an underscore indicates it's part of Python's private API, not its public API.

red solar
#

I’m making a PR to make it public tho

raven ridge
#

might be worth a search on the bugtracker to see if there's any comments about why.

red solar
#

I made a post to the capi-sig mailing list, they said it was ok πŸ€·β€β™€οΈ

#

(Well one guy did)

unkempt summit
paper echo
#
itertools.product(*preseq)

@unkempt summit like this?

red solar
#

Oh u guys moved here lol

paper echo
#

was this a followup from a help channel?

red solar
#

General channel lol

paper echo
#

oh

#

i didnt move anywhere

#

!e ```python
from itertools import product
preseq = [(1, -1), (2, -2), (3, -3), (4, -4)]
postseq = list(product(*preseq))
print(postseq)

fallen slateBOT
#

@paper echo :white_check_mark: Your eval job has completed with return code 0.

[(1, 2, 3, 4), (1, 2, 3, -4), (1, 2, -3, 4), (1, 2, -3, -4), (1, -2, 3, 4), (1, -2, 3, -4), (1, -2, -3, 4), (1, -2, -3, -4), (-1, 2, 3, 4), (-1, 2, 3, -4), (-1, 2, -3, 4), (-1, 2, -3, -4), (-1, -2, 3, 4), (-1, -2, 3, -4), (-1, -2, -3, 4), (-1, -2, -3, -4)]
unkempt summit
#

@red solar oh sorry dood, didn't see your message

red solar
#

It’s ok salt answered it well

unkempt summit
#

@paper echo @red solar thanks i appreciate the help

#

what exactly does the asterisk mean?

#

i know when you print a list e.g. print(*list), it will print out the strings rather than a list of strings on one line

#

ah, okay I get it

paper echo
#

@unkempt summit it "unpacks" the iterable, passing each element into a separate argument in the function

unkempt rock
#

This stuff makes python so weird and I don't know if I love or hate that... Even if there are great use cases

shut anchor
#

[*range(10)], for example, would give you a List of ints from 0 to 9. You also see it in functions.

    for argument in largs:
        print('I am processing an argument')``` Where you can call `example('apple')` or `example('apple', 'pear')` @unkempt summit
#

Then there's keyword argument double stars, where you define functions like

    return kwargs``` If you feed it `example(a=apple', b='pears')` it returns `{'a':'apple', 'b':'pears}`
radiant scroll
#

I'd like to create a class which acts like an instance for itself directly. Let's say I want to subclass list like this:

class LineColor(list):
  def __getitem__(self, index):
    try:
      return super().__getitem__(index)
    except IndexError:
      return (255, 255, 255)

LineColor[2] # This should give (255, 255, 255)

Obviously, this won't work and will raise TypeError

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'type' object is not subscriptable

But is it possible to do it?
Can you have a class that you can use directly, just like an instance of it?

Note: I know it's not a great idea to do it, but I just wonder if it's possible, and if so, how?

shut anchor
#

and you can have positional arguments in your defs, too, example(arg1, arg2, *largs) but the starred stuff has to come after

#

and you can feed starred and double starred stuff into other function calls, too. example(*largs) example(**kwargs)

brave badger
#

@radiant scroll First option that I can think of is making it implicitly a singleton object e.g.

from collections import UserList


class _LineColor(UserList):
    def __getitem__(self, index):
        # ...


LineColor = _LineColor(...)
gleaming rover
#

you could use a custom metaclass

radiant scroll
#

yeah, that would be an option, but I was thinking to do this directly with the class, without even making an instance of it

gleaming rover
#

and override the __getitem__ of the type

radiant scroll
#

how can I do that gm?

#

I heard about metaclasses before, but I never got to use them

gleaming rover
#
class GetitemMeta(type):
    
    def __getitem__(self, value):
        return 1
    
class Concrete(metaclass=GetitemMeta):
    pass

Concrete[0]
#

you need to do some other stuff though, to make it work with your class

#

which on the whole would make it kinda not worth it if you're not already familiar with metaclasses

#

like I think a singleton or some other abstraction would work much better

radiant scroll
#

yeah, in practice certainly, but this is more for just my curiosity

#

I'd just like to know how to do this, as I said in the first message I don't intend to use this right now, but I'd like to know how it works

#

so, if you have the time, what other stuff would I need?

#

but I get it if you don't

deft pagoda
#

you can also use __class_getitem__ for that particular example instead of a metaclass

#
In [280]: class LineColor:
     ...:     def __class_getitem__(self, key):
     ...:         return 255, 255, 255
     ...: 
     ...: 

In [281]: LineColor['hi']
Out[281]: (255, 255, 255)
gleaming rover
#

oh yeah I forgot that was a thing

#

it's used in typing IIRC

radiant scroll
#

can I replicate full list behavior with that?

#

like __class_setitem__ etc?

deft pagoda
#

maybe, i'm not sure exactly what you want --- if you want exact list behavior you could just subclass list

paper echo
#

yeah but you can't use it to define custom generics 😒

red solar
#

Dependent types > generics, change my mind

deft pagoda
#

to be fair, i've use metaclass getitem more than class_getitem

#

because i've used it once

paper echo
#

apparently there are a lot of issues with dependent types

#

lol

#

what did you use it for? curious

red solar
#

Why are you both salty...

paper echo
#

i could see class getitem being useful for defining some kind of fun shared caching semantics

deft pagoda
#

use it as an alternate constructor for ranges

paper echo
#

oooh so you can use slices

#

clever

#

why metaclass and not class getitem?

deft pagoda
#

because i wanted the class logic to be mostly related to interval mathematics -- all the other stuff i put in a metaclass

paper echo
#

interesting

deft pagoda
#

i've thought about undoing it though

paper echo
#

i like this alternate constructor because you can easily subclass it to obtain things like np.arange

radiant scroll
#

interesting, well, thanks a lot guys. I'll have to take a deeper look into metaclasses, they seem very interesting

unkempt rock
#

Just question, appending to @radiant scroll s one, what you use metaclases for?

chrome oak
#

z

#

z

trim bronze
#

Can advanced Python be used to program a motherboard circuit?

tawdry gulch
#

@unkempt rock metaclasses are used to make classes

#

As you can see here, Par is of type type, because it is an instance of class type. This makes type a metaclass

#

Correct me if I am wrong anyone

deft pagoda
#

that's right, when you make a metaclass, you inherit from type

#

typically

true hollow
#

yikes
there is spaces

undone hare
#

isinstance()Β is one of the most called functions in large scale Python code-bases (by static call count). In particular, when analyzing some multi-million line production code base, it was discovered thatΒ isinstance()Β is the second most called builtin function (afterΒ len()). Even taking into account builtin classes, it is still in the top ten. Most of such calls are followed by specific attribute access.
From PEP622

#

That's interesting

raven pike
#

isinstance is used that much because it is dynamically typed?

charred wagon
#

That sounds unpythonic, especially cause it points it it's followed by attribute access

#

Anyone ever heard of duck typing? Geez

undone hare
#

I think the main issue is that we don't have operation overloading in python, so you have to check the argument type to choose how you are going to process it

rugged lake
#

yes PEP 622 is some weird shit

#

it was felt that it might be better to acquire some real-world data on how the match statement will be used in practice before moving forward with some of these proposals
literally no one would use it

brave badger
#

How so?

red solar
#

They seem confident people would use it, especially if what they say about isinstance is true

flat gazelle
#

in actual business logic, nested structures are very common

#

this is very convenient for dealing with them

visual shadow
#

I wonder if it's also simply because people want to present a unified interface capable of accepting multiple datatypes. The real work has to be delegated separately based on the datatype received.

#

I'd imagine that as the biggest reason why such a thing would happen, but I'll openly admit I'm still honestly shocked at that finding

#

Like, I feel like there's so many more candidates that should have been more common, at least print and open for example

flat gazelle
#

print is almost never used in real applications, logging is done differently and you generally only open one or two files

#

it will not see as much use as it does in haskell/elm/*ML because you do not use it to make recursive functions, but all the other applications still hold. Java is also getting a similar system, despite java having a different way to do this with inheritance trees (those are also a thing in python, but are a bad idea to use as much as in java)

raven ridge
#

"even taking into account builtin classes, it is still in the top 10" is an interesting clause there. Python has only ~70 builtin functions and classes per the table at https://docs.python.org/3/library/functions.html#built-in-functions - and isinstance being used more than 85% of those doesn't surprise me in the least; many of them are very rarely used .

#

It shouldn't be shocking that isinstance is used more commonly than divmod, issubclass, eval, hex, ord, chr, etc etc. I'd go so far as to say that many of those have no business being in the builtin namespace, if it wasn't for backwards compatibility.

deft pagoda
#

hmm, i never really use ord, chr -- except for aoc

#

so is this switch case with pattern matching?

unkempt rock
#

tes

cloud crypt
#

It’s so hard to find an idea for a library that implements something that was not done before

open trout
#

I usually just make an api for a website and call it a day

#

like, consider urbandictionary

#

I have an idea for a library though. A game library that spams decorators everywhere and somehow makes it work

tawny shoal
#

I'm looking forward to pep 622!

lost nexus
#

I hope it passes. I really yearn for switch/case functionality.

tawny shoal
#

I also have a lot of isinstance calls in my framework since I allow users to wrap Discord objects in Django model objects and use them mostly interchangeably

#

only that if wrapped they have a couple more DB related features

#

So with match I could just group behavior easily and it's much more pleasant to read

#

I like Rust's match operator

gray mirage
#

Oh, that looks like kotlin's when statement

#

It'd be interesting to see something like this in Python

#

The PEP seems very switch-centric though

tawny shoal
#

Yeah

#

The cool thing about Rust's match is that you always have to handle all cases

#

Prevents a whole bunch of errors

gray mirage
#

Yeah, same in Kotlin, but I don't think python is rigid enough for that

tawny shoal
#

If you don't handle all cases it won't even compile

#

yeah it's not

#

I've taken quite a liking to using Rust together with Python recently

radiant fulcrum
#

ye o3 is pretty good

glass robin
#

Would we have case class then? Like in Scala?

lost nexus
#

I'd expect it to be something like

match type(obj):
  case str:
    ...
  case int:
    ...

Take this with a grain of salt though.

flat gazelle
#

case classes are probably not really suited to python. Python is not very suited for complex inheritance

cloud crypt
#

composition over inheritance

radiant fulcrum
#

I feel like this is gonna be the best area to ask seeing that we havent got the native lib channel yert

#

so

#

How do we handle Futures or async in general in C and C++ which is then handled by python

magic python
#

@cloud crypt there seems to be a lot more for social science in R than python... so - if you wanted something to develop then something along that line might use useful πŸ˜„

subtle pike
#

I can't understand the benefits of R

#

I have to use it for my psychology course and it's horrendous

minor sinew
#

@radiant fulcrum do you mean you're spawning threads in c/c++ and you want to get the result of each thread in python?

deft pagoda
#

composition over inheritance is the worst mantra ever

red solar
#

How so?

#

I think it’s fairly helpful

deft pagoda
#

i think inheritance is most helpful

red solar
#

Sure

#

But unless you specifically need what inheritance gives you over composition, i’d still always go composition

deft pagoda
#

the only composition that matters:

class MyClass(Parent, OtherParent):  # "composition"
red solar
#

πŸ˜‚πŸ˜‚

#

The issue is that inheritance can be abused in a way that composition can’t

#

And the easiest way to avoid that is composition over inheritance

magic python
#

@subtle pike it was designed to work with data, there are lots of statisticians that work with it, there are many libraries existing for working with stats, it has a fantastic plotting library, etc

deft pagoda
#

composition can be abused, because people could just use a simpler inheritance

red solar
#

I folder with a list of .py files - another .py file outside the folder creates a python file that inherits from every single class in that folder, and then it imports that python file and uses that - can you abuse composition that badly? @deft pagoda

subtle pike
#

@magic python the plotting library is featureful but is pretty easily replaced with python libs. The data science aspect is accurate, it does have a lot of built-in vector operations but python + scipy/pandas also achieves this. All I see is that with R you have a language that's a nightmare to debug and use without the documentation/community of python

#

Every academic I have ever spoken to about R agrees it's a language of chaos

#

the syntax is bad and the language is far from convenient

magic python
#

idk if this is on topic ?

#

but i disagree

deft pagoda
#

i think R is one of the most liked languages of all languages, though I know nothing about R -- and this is definitely off-topic,

red solar
#

i have never heard that

deft pagoda
red solar
#

idk how i feel about go being so high up

stone geyser
#

not really sure if this is a question for this channel or not, but is there a way to see if the python file being run is in a zip or not?
meaning, like downloads -> ZIP -> script.py

north root
#

that's not a question particularly for this channel, but i don't think that's possible since the file is unzipped before run

stone geyser
#

more-so looking when being run as an executable

#

i figured it out

north root
#

ah alright

tawdry gulch
#

@deft pagoda that shouldn’t be how you use composition. Imo it should be something more like:

class Q(composer):
pass

Q.attach( factory_part1 )
Q.attach( factory_part2 )
etc.
And have specialised behaviour that deals with composition in a better way than just inheriting methods

#

Obviously, the downside is that the development team and anyone reading your code will have to be made aware of the (sort of different?) paradigm you are using

deft pagoda
#

i don't know what you're talking about

tawdry gulch
#

Nevermind! I just realised you were joking lol

red solar
#

@tawdry gulch elaborate a bit more on that?

#

not seen it before

#

(i think gdude may have tried to explain it to me once a while back)

strange fog
#

from https://www.mail-archive.com/python-dev@python.org/msg108627.html

def http_error(status):
     match status:
         case 400:
             return "Bad request"
         case 401:
             return "Unauthorized"
         case 403:
             return "Forbidden"
         case 404:
             return "Not found"
         case 418:
             return "I'm a teapot"
         case _:
             return "Something else"

isn't case _: kinda ugly? why not just else:?

#

or at least case ...:

deft pagoda
#

i don't mind case _:, but else: does also make sense to me

strange fog
#

nice :p

red solar
#

nice to see other people fighting for it so i don't have to lol

paper echo
#

i just hope this doesnt get rushed through

#

i like this version

    case Point as (x, y):
        print(f"Got a point with x={x}, y={y}")
void sonnet
#

i just realized. pylint is going to have a field day with all those returns. #ThereCanBeOnlyOne πŸ˜„

#

though, i'm confident they'd get it straightened out fairly quick after it goes live (if).

tacit hawk
#

That really makes clear the distinction of variable lookup and variable binding. With Point(x, 0), x looks like a lookup

#

That would be better than add an operator for each binding, no?

raven ridge
#

I liked the suggestion of case Point with (x, y): better than case Point as (x, y): - we want to match a point with some properties.

final mica
#

Anyone here know Flask? I'm trying to get it to route to a /tools page, but it keeps redirecting to /tools/, which messes up future redirects...

#

Just wondering if there's a rule against /tools pages or something, or if it's likely to be a bug in my code that I just haven't been able to find yet.

final mica
#

(Of course, right after I post, I figured it out. It's not a flask problem, it's a livereload problem - so I just stopped using it. It seems to have caused more issues than the one problem it fixes somewhat)

grave jolt
#

@final mica in the future keep in mind that this is not a help channel. Please read the channel description.

narrow nexus
#

can u guyz hav a look here @void sonnet

slim island
#

Also don't ping random people

radiant fulcrum
#

@minor sinew Sorry for the latest response, no its not quite what i ment

#

Currently Im trying to work with Futures in both Rust and Python

#

But binding / linking would be rather hard without understanding how the underlying Cpython handles futures itself with asyncio

#

thats sorta what i was getting at

loud lodge
#

.fool

worldly venture
#

wow 3 new peps in the span of 4 days

unkempt rock
#

Greetings

worldly venture
#

@unkempt rock care to explain that?

lost nexus
#

what are the new peps?

last pollen
final whale
#

Internally, what makes a while loop different, compared to a recursive function ?

hollow crane
#

a recursive function adds to the call stack, and you can only go so far before you reach a limit

full jay
#

Essentially recursive functions have to remember where they were called and what the values were in that scope at the time. Where as a while loop literally just keeps on going down a straight path (albeit one that loops back to a certain point)

#

Call stack is a funky thing to understand at first

final whale
#

got it, thank you for redirecting me

#

onto reading about call stacks

torn plank
#

does running socket library and selenium cause problems?

#

like running one after the another in the same program.

hollow crane
#

not that i've seen

#

i would imagine you're doing something weird with threading to cause that maybe

#

can you show your code

torn plank
#

No, I a not using threading. I have a different python program for port scanner, and selenium starts executing after the port scanning is complete. I have this complete explained in #help-falafel

#

@hollow crane here is the code. Please @ me if you reply. Thanks

fossil jetty
#

doesn't the case statement already exist as a dict mapping?

match thing:
    case a:
        return a_stuff
    case b:
        return b_stuff
things = {a: a_stuff, a: b_stuff}
return things[thing]
brave badger
#

match/case should allow for different types of patterns iirc so it's a bit more flexible

torn plank
tacit hawk
#

Dict requires the matches to be hashable, no?

spice pecan
#

yeah

short mango
#

any python project website like jetbrains academy

narrow kettle
#

Does python do any recursive call optimizations

visual shadow
#

Nope

#

So favour iteration over recursion if both are equivalent, the iteration will probably perform better.

paper echo
#

does pypy have tail call optimization?

flat gazelle
#
>>>> def fun(a):
....    if a:
....            return fun(a - 1)
....    else:
....            return 9
....
>>>> fun(1000000)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in fun
  File "<stdin>", line 3, in fun
  File "<stdin>", line 3, in fun
  [Previous line repeated 2334 more times]
  File "<stdin>", line 2, in fun
RecursionError: maximum recursion depth exceeded
```seems no
red solar
#

Try switching the if/else around?

#

(Probably won’t make a difference but maybe)

flat gazelle
#

did nothing

#

numba also fails to optimize tail calls

tawdry gulch
#

That’s a rip

paper echo
#

😦

deep willow
#

https://paste.ofcode.org/zsqnBH2pyG2Hz3menbmm9h

Can anyone help me figure out why any number below 100 or above 500 in line 8 does not return properly instead is just spammss the numberr

for example if its 501 then i get
501
501
501
501

and so on ,also this is not the whole code, i'm template matching from my screen.

grave jolt
#

@deep willow This is not a help channel, please read the channel description. If you want to get help with a specific question, check out #β“ο½œhow-to-get-help.

deep willow
#

oh srry i read it just didnt understand it i do now thanks

boreal umbra
#

There's a suggestion to have set.add return a bool depending on certain things, and I said that that change would have to be part of a larger discussion about what mutator methods should return if they don't return None

#

I'd want to see them return self

#

If someone has been writing code that uses what mutator methods return up to this point, they deserve for that code to break anyway.

red solar
#

swear there's some other datatype where adding an element returns a bool

boreal umbra
#

I remember in C, a lot of data structure functions return 1 if it worked and 0 if it didn't, since you don't get exception handling in C.

#

In fact I think accessor methods work the same way, and you'd give them a pointer of where you want it to put the accessed item.

#

oh C

red solar
#

lol nah return -1 and set errno, surely?

#

but .i was more talking about in python

#

maybe i'm imagining things, and i was just coercing None to False

peak spoke
#

Not a fan of that with python's design, and it would require changes in a lot of places along with confusing for non 1st party libs that do it like it's now

boreal umbra
#

If someone commits their venv folder to their git repo (I didn't do it), isn't the storage footprint of the repo permanently significantly larger unless they delete that commit?

charred wagon
#

Yes unless a commit undoes (not the same as rewriting history and deleting the commit) it and a shallow clone is done

boreal umbra
#

I suppose. That was my only question on that.

worldly pebble
#

Any good resources to get started to cython apart from documentation

open trout
#

It feels like python if flowing more and more toward something like java

#

the push with classifying a lot of stuff (dataclasses, enums), type hinting, and now the case/switch-ish PEP, and probably some things I forgot

#

Do you guys agree? What do you think about it?

brave badger
#

imo dataclasses are quite the opposite of Java classes given the fact that they're boilerplate killers

open trout
#

right, but in the sense that it's something that you earlier would've done with a more primitive type or a namedtuple

brave badger
#

Fair point

#

Well at least to me, dataclasses remind me more of Haskell's record syntax

open trout
#

I love dataclasses, don't get me wrong, but I think namedtuples are peak pythonic

#

(I almost never use namedtuples though... lol)

brave badger
#

This too is pretty awesome

> Namedtuples and dataclasses will have auto-generated __match_args__.
> For dataclasses the order of attributes in the generated __match_args__ will be the same as the order of corresponding arguments in the generated __init__() method. This includes the situations where attributes are inherited from a superclass.
open trout
#

feels good man

brave badger
open trout
#

another decorator hack

#

I am pleased

red solar
#

If anything dataclasses are like java records I think

unkempt rock
languid dagger
#

@unkempt rock Please don't advertise your channel/question elsewhere, just wait patiently, if someone can and wants to help they will

cunning crest
#

any suggestions for extracting data in large scale from PDF ?

subtle pike
#

@brave badger that looks gnarly

#

i always end up with a type alias like T = Union[A, B, C, ...] at the bottom of a scary looking types.py file

paper echo
#

@red solar dataclasses are a crippled version of attrs classes

grave jolt
shy vine
#

This is not a help channel

unkempt rock
#

alr

#

can u help here pls

shy vine
#

You don't need to advertise your help channel

#

Everyone can see them

unkempt rock
#

alr sry

#

mb

lethal prism
#

Hi, I am stuck in some python code, I am doing this just for fun and to learn about tensor flow, Keras and image classification, But I can get an improvement on my accuracy....maybe you can give a tip or an idea here is my code https://paste.pythondiscord.com/obixuyixuf.py

unkempt rock
#

@lethal prism I don't know anything about TensorFlow, but one tip for convenience and readability is to bunch multiple import statements that grab from the same library into one statement.

#

For instance, I notice that you are importing a lot of things from keras.layers. You could rewrite all of those import statements into one with from keras.layers import Sequential, Conv2D, MaxPooling2D and so on.

empty cape
#

How to send notification on Android using PYTHON?

lethal prism
#

@lethal prism I don't know anything about TensorFlow, but one tip for convenience and readability is to bunch multiple import statements that grab from the same library into one statement.
@unkempt rock Thank you for this

unkempt rock
#

NP

true hollow
tribal linden
#

4

molten onyx
#

No. PyPy follows the Python language design, including the built-in debugger features. This prevents tail calls, as summarized by Guido van Rossum in two blog posts. Moreover, neither the JIT nor Stackless change anything to that.

tacit hawk
#

How can I make logging.exception() log only the first 2 frames of the traceback?

charred wagon
#

You may need to use the traceback module to limit it

#

And explicitly pass the exc_info tuple with the modified traceback

#

I've not tried that but I feel like the traceback module would have something relevant for you

tacit hawk
#

Is it possible to change de default logger class? I would like to overload exception

charred wagon
#

You could just monkeypatch it I believe

tacit hawk
#

I think that is enough

forest flicker
#

does web-assembly means front-end web app can be written in Python in future?

red solar
#

i don't see how wasm would help with writing a front-end web app in Python?

raven ridge
#

You need to convert a Python interpreter to webassembly first, and then you can use it to run arbitrary Python code, from what I understand.

red solar
#

you can't just write a python interpreter in JS?

raven ridge
#

technically, sure. Practically, wouldn't be a great use of time.

red solar
#

i'm sure someone's written a c to js transpiler

#

now i wanna try and find it and try it on the cpython repo

raven ridge
#

couldn't say, I avoid JS like the plague πŸ™‚

red solar
#

lmao dog keeps getting deleted (try in off topic channels)

#

same (except maybe typescript)

true ridge
red solar
#

oh shit

#

purple

#

@true ridge what's with lack of atomics in python? just no interest?

true ridge
#

What do you mean by atomic?

red solar
#

(but in python)

#

(and possible abstracted to a higher level)

true ridge
#

I am not familiar with the concept but if I understood correctly, GIL would prevent race conditions for all python level objects (at least in cpython) so there wont ve any need for that types.

red solar
#

1, isn't GIL an implementation detail rather than a language feature? and 2, what about items in shared memory, accessed by multiple processes?

true ridge
#

GIL is in an implementation detail (it is why I specified at least in cpython, but other implementations should be responsible for preventing such race conditions regarding all objects).

#

I am not familiar with the shared memory implementation, so no answer to that.

red solar
#

is there any reason you know of why cpython wouldn't be open to the suggestion of an atomic library?

true ridge
#

No, I am not familiar with the concept

red solar
#

ok i'll make a suggestion to python-ideas once i have a complete library

visual shadow
#

Uhm

#

I think python operations are already atomic

#

Most of the builtin methods, translate to a single operation on a dis.dis breakdown. So such a library would be redundant at best.

#

And if it's a custom operation you are doing, then you're already ideally using locks as usual

#

These statements are built on the premise that I understood atomic correctly, which may or may not be true πŸ˜…

gleaming rover
#

Most of the builtin methods, translate to a single operation on a dis.dis breakdown. So such a library would be redundant at best.
@visual shadow being a single opcode on the Python level doesn't mean that they're atomic on a processor level

strange fog
#

what's wrong with just

with lock:
  # do thing

@red solar ?

red solar
#

it's a lock...

#

lock has additional overhead, and also not sure how you'd put a lock in shared memory?

strange fog
#

oh. shared memory? like IPC shared memory?

red solar
#

yeah

strange fog
#

each OS has their own interprocess synchronization primitives