#internals-and-peps

1 messages Β· Page 48 of 1

deft gyro
#

what

peak spoke
#

Usually easier to just subclass UserList etc. with design and behaviour

spark magnet
#

@lost nexus the problem with subclassing builtins like that is it is easy to slip back to a real list: l = MyList(); l = l[:3] # oops, l is list, not MyList

lost nexus
#

I see

unkempt rock
#

hey

#

I'm getting this error

#

when running headless chrome via python script

grave jolt
#

Please read the channel description @unkempt rock

#

This is a discussion channel, not a help channel

#

Hm, maybe it's possible to make a member deriver.

unkempt rock
#

member driver

#

the script is working good via chrome non -headless mode

grave jolt
#

Oh, I'm talking about the inheriting list problem

unkempt rock
#

okay,

primal magnet
#

Event: ???

grave jolt
#
@derive
class MyList1(list):
    # slices return  MyList instances
    pass 

@derive
class MyList2(list):
    # slices return  MyList instances
    # `update`, `__add__` and `__iadd__` are redefined to use the new `append`
    
    def __init__(self, iterable):
        self.container = list(iterable)

    def __setitem__(self, index, item):
        self.container[index] = item

    def append(self, x):
        self.container.append(x)
primal magnet
#

H m m

true ridge
#

UserList is slightly more useful then subclassing since it always wraps the result to the subclass (via self.__class__)

grave jolt
#

Will it work correctly if I implement, say, an immutable linked list?

gloomy rain
#

How come dict.keys() returns KeysView[Optional[key_type]] rather than KeyView[key_type]?

#

Or, at least, mypy seems to think so.

#

Oh, nevermind, it was just the type inference acting up.

cloud crypt
#

ah

peak spoke
#

Typeshed has KeysView[_KT]

cloud crypt
#

^

#

or at least it should

#

mypy is pretty dumb at times ngl

gloomy rain
#

Yeah, it wasn't an issue with the return type, it was mypy inferring an optional type on the result of a dict comprehension.

cloud crypt
#
def whatever(
    ...
) -> return_type:
    ...``` _no return type of the func_
gloomy rain
#

In my code.

cloud crypt
#

and I don’t want to fix my code just by # type: ignoring all its mistakes

gloomy rain
#

Sometimes, although in this case it was actually correct and it was my fault.

cloud crypt
#

indeed, but I have shown an example that makes me wonder above

#

ever tried to make a function that takes str and bytes and calls their methods? that’s helly

#

well unless you use typing overload

merry pollen
#

honesty i have p good experience with mypy 90% of the time

cloud crypt
#

I mean

#

mypy just gives up if you do some magic like _ctypes.PyCFuncPtr

unkempt rock
#

How do i make an os with python?

visual hatch
#

What are functions Annotations?

I recently been trying to learn a little bit of functional programing in python and I came across the word annotations. After searching and watching few videos I still didn't get a clear view of what are the function annotations and why they could be useful in python. I'll be very thankful if you guys answere the following questions in a simple way to let me get a better understanding of it.

  1. What are functions annotations used for?
  2. What will it effect on my functions parameters if I used them in function?
  3. Does they effect in any manner at my function?

I'll be really happy if you guys could explain these to me or maybe share some youtube videos which help me understand these.

Thank you so much.

visual shadow
#
  1. They are the groundwork on which type hints are born. A way to add additional info about parameters and return values
unkempt rock
#

Can anyone suggest me a good python machine learning tutorial?

cloud crypt
#
  1. nope, as annotations are optional
unkempt rock
#

See on udemy

visual shadow
#

Which means, the big thing is this: function annotations do nothing for the code execution. They're mostly there for the humans who are reading the code

cloud crypt
#

even though there are static type checkers like mypy, which is their de-facto

visual shadow
#

Once that part clicks, the rest is easy to understand

cloud crypt
#

you can hook your function to dynamically check inputs based on annotations, but that's just ew

#

you can have use of annotations like discord.py does, though

visual shadow
#

So 2. And 3. Are nope in native python. You can have libraries or programs built on top of it that tries to turn them into something more, as nekit mentioned

#

But keep them aside when you first try to comprehend function annotations themselves.

peak spoke
#

Their only effect is that they're added to the __annotations__ attribute

cloud crypt
#

also annotations can be used in order to create something like a dataclass

#

yeah

#

I once wrote some thing for ctypes with it ```python
from ctypes import wintypes
import ctypes

kernel32 = ctypes.WinDLL("kernel32")

@func_def(kernel32.CreateToolhelp32Snapshot)
def create_snapshot(flags: wintypes.DWORD, process_id: wintypes.DWORD) -> wintypes.HANDLE:
pass```

#

which sets the argument/return types for DLL function in order to make calling it simpler

#

~ Finally read about descriptors in python, haha ~

raven pike
#

yesterday i checked out function scopes and global etc

#

i'm mainly using pandas, and when i change outer df in my functions it actually changes

#

shouldnt it be changed at all in the end?

#

im confused

cloud crypt
#

can you show an example?

visual hatch
#

I got it now so basically they are the hints for human. To better understand what types of value function will take and what it will return.

#

I was thinking its somekind of way to make functions take only a specific types of values but I was wrong as that is not the case

charred wagon
#

They are used by mypy, which is a tool that statically checks that the types are correct

#

But it doesn't enforce anything at runtime

raven pike
#

@cloud crypt i'll post now, one min

#
df = pd.DataFrame([0,1,2,3])
0
0    0
1    1
2    2
3    3

def example(df):
    df.iloc[0] = 99
    return None

example(df)

df
0
0    99
1    1
2    2
3    3
#

@cloud crypt

cloud crypt
#

what's confusing in it

#

ah

#

well

#

DataFrame is mutable

#

and you mutate the object

#

this has nothing to do with scopes really

#
lemon = [1, 2, 3]

def append_n(some_list, n):
    some_list.append(n)

append_n(lemon, 4)

print(lemon)  # [1, 2, 3, 4]```
raven pike
#

so scope is just for variables?

#

protected that way

#

so... global keyword is just for variables in order for them to be manipulated?

flat gazelle
#

Yes. Variables are names of objects and are affected by scope. Objects themselves are not

raven pike
#

hmmm, good to know. i was always worried if my pandas object would be changed or not when entering a function

#

if i dont want them to be changed, then using .copy() would be a good practice for me

#

when passing in funcs

#

thanks @cloud crypt @flat gazelle

tacit hawk
#

well functions that change the object's state could be owned by the object

unkempt rock
#

you guys are talking magic

tacit hawk
#

to be a safe copy, you would need to do a deep copy. I think that is costly

unkempt rock
#

is pycharm a good text editor for python beginners?

tacit hawk
#

pycharm is an IDE, it comes with a bunch of features

#

I use visual studio code

raven pike
#

@tacit hawk yeah deep copy but some computational stuff needs to be done in a big dataframe, so i have to use copy of a section to do various stuff

#

some of them might change the original, that's what i'm afraid of

proper narwhal
#

@tacit hawk ide is like a tool kit and one of the tools is a python compiler??????

tacit hawk
#

Idk if pycharm installer comes with python or you need to install it. But yes, IDE is a collection of tools that helps you, it is not just a text editor

craggy bane
#

If I recall, pycharm comes by itself. You install Python, then you tell Pycharm where it is.

#

I apologize if the above information is inaccurate. I don't use Pycharm anymore. I now use NotePad++ then use Powershell to compile and view the output.

near coral
#

props to you for using Powershell

#

wish I could tolerate it

craggy bane
#

It took a bit of setup and I had to learn some Powershell syntax. It was a bit of a pain initially, but after I used it for awhile, I don't think I wanna go back to an IDE except for debugging or other resources.

crisp wharf
#

Hello, im a student trying to learn python by myself. I have worked with Flask, Docker, REST API + Postman, MongoDB, PostgreSQL , automatic tests and doing DJANGO project now. Are there any other popular libraries/ solutions worth learning?

craggy bane
#

Props to you. You've gotten your feet wet in more things than I have. As for things "worth learning", I just go with what interests me. I'm not too familiar with the trends of the industry, even though I probably should be.

crisp wharf
#

Thanks, could you give some examples? Might be interesting for me too

hollow crane
#

do lots of little projects that interest you

#

new apis and maybe some new ways of thinking you'll have to encounter

raven pike
#

Check stack overflow surveys

#

Then go after what's popular if that's what you are after for

unkempt rock
#

Hello, im a student trying to learn python by myself. I have worked with Flask, Docker, REST API + Postman, MongoDB, PostgreSQL , automatic tests and doing DJANGO project now. Are there any other popular libraries/ solutions worth learning?
@crisp wharf By automatic testing do you mean pytest and unittest?

north root
#

@crisp wharf have you deployed to production before?

#

this is off-topic for this channel, however

magic python
#

@tacit hawk you can't copy matplotlib axis objects - not sure if you're aware of why that is?

i tried recently with deep copy, and it failed, so you saying that above reminded me

tacit hawk
#

No I am not aware, I don't know that lib

night patrol
#

I've an idea

#

I want to build an fps counter

#

Do you guys have any idea on the best way to capture the frames of the display?

#

What I'm thinking is to capture 1 pixel from the screen using opencv, is it efficient idk, maybe?

alpine cedar
#

What I'm thinking is to capture 1 pixel from the screen using opencv, is it efficient idk, maybe?
@night patrol maybe more than one

#

what if the pixels stay the same

grave jolt
night patrol
#

oh sorry

merry pollen
#

uh hmn. so, back before delayed type annotations, you could pass in functions as type annotations

#

but now they're strings & the suggested method to get objects back from annotations is eval, which doesn't seem to be cooperating easily with functions

#

so my question is, is there a convenient means of embedding functions into delayed annotations

#

and/or get a function back via eval()

#

oh hmn okay jk apparently annotations take care of all of that if you pass in the annotation as a string

#

...weird

#

i guess pycharm just hasnt gotten the linting down for that yet

raven ridge
paper echo
#

im not sure what "incompatible with PEP 484" would mean

#

presumably some of the experimental libraries that use type hints for runtime validation

merry pollen
#

@raven ridge is what one would think, but pep 593

#

so 3 years ago maybe

#

but given pep 593's acceptance, runtime annotations are back on the menu

lost nexus
#

is there a max amount of references that an object can have?

north root
#

probably until you run out of memory

raven ridge
lost nexus
#

haha alright

prime estuary
#

Which is also the maximum amount of addressable memory, so you'll hit the OS ram limit before being able to have that many objects to reference it from.

raven ridge
#

Technically it's one byte less than half the maximum amount of addressable memory, and technically it's possible to add a reference without using any additional memory.

#

It's possible to reach that max reference count without running out of memory first, though very very unlikely. Absent a bug, it would require externalizing pointers out of the process with the intention of reading them back in later for the decref call, or something like that.

#

But I wouldn't be shocked to learn that Python doesn't guard against that, since you have to really be doing something weird (or forgetting decref calls, meaning you have a bug) in order to get that to happen

raven ridge
undone hare
#

!pep 611

fallen slateBOT
#
**PEP 611 - The one million limit**
Status

Draft

Created

05-Dec-2019

Type

Standards Track

undone hare
#

This PEP is about adding a one million safe limit to most things in the interpreter

wide shuttle
#

I've been looking into https://www.python.org/dev/peps/pep-0518/ recently (pyproject.toml) and came across a blogpost by Brett Cannon about the subject. There's nothing really groundbreaking in the post, but it's nicely written and provided a coherent story on the why and what: https://snarky.ca/what-the-heck-is-pyproject-toml/

Tall, Snarky Canadian

Recently on Twitter there was a maintainer of a Python project who had a couple of bugs filed against their project due to builds failing (this particular project doesn't provide wheels, only sdists). Eventually it came out that the project was using a pyproject.toml file beca...

wide shuttle
#

Hey, @magic junco, this is not a help channel, but rather a discussion channel to discuss Python itself, from higher-level perspective. We do have help channels and you can claim one for your question (see #β“ο½œhow-to-get-help). Sorting algorithms also sounds like something that could fit in the #algos-and-data-structs topical channel.

magic junco
#

oh, sorry about that

cedar nimbus
#

can we get switch-case with python 4?

undone hare
#

!pep 3103

fallen slateBOT
#
**PEP 3103 - A Switch/Case Statement**
Status

Rejected

Python-Version

3.0

Created

25-Jun-2006

Type

Standards Track

molten onyx
#

Guido is adding it in actually

undone hare
#

Status : Rejected
Probs- wait what

molten onyx
#

also there won't be 4.0 afterall

#

they got a new parser

#

sec

undone hare
#

Ah that's true

molten onyx
#

he said so explicitly in a talk I forgot which

#

and also

undone hare
#

I wonder why LL(1) can't parse it, the proposal seems to be parsable

molten onyx
#

Parsing Expression Grammars (PEGs) are a relatively new formalism for describing grammars suitable for automatically generating efficient parsers. I've become interested in using a PEG-generated parser as an alternative to CPython's nearly 30 year old "pgen" parser generator. ...

β–Ά Play video
flat gazelle
#

match statement may be more powerful than switch case

molten onyx
#

yes

#

definitely

cloud crypt
#

match is cooler yeah

cedar nimbus
#

idk really since i didnt even know anything about it

flat gazelle
#
match [1, 2, 3]:
    case [1, 2, a]: print(a)
    case [*b]: print(b[1])
``` maybe
#

or maybe a less powerful form

#

full pattern matching seems a bit unneeded

cloud crypt
#

typo in match

flat gazelle
#

ty

cloud crypt
#

soft keywords sounds neat

#

multi line lambdas when lemon_pleased

unkempt rock
#

is there any way to create my own low level programming language?

#

thanks in advance guys

cloud crypt
#

not quite related to this channel?

cedar nimbus
#

you can make functions in python

#

and classess

unkempt rock
#

i dont have any idea of this topic

cloud crypt
#

also what do you define as low level programming language

unkempt rock
#

not high as python

#

where should i ask it ?

cloud crypt
#

well not as high as python is pretty much most of the languages

unkempt rock
#

but then certain languages like ruby,r etc?

#

also html!?

cloud crypt
#

whatever

unkempt rock
#

is there any way to?

molten onyx
cloud crypt
#

question is why, and you wouldn't want to use python for that unless it is a toy language or something

unkempt rock
#

nono

#

i am good with python

#

just now completed my machine learning course

flat gazelle
#

Most boring, useful languages are written in C.

unkempt rock
#

and looking forward

#

i just want to experiment on something different rather than taking my personal projects in python

flat gazelle
#

honestly, high level languages are easier, because you can avoid LLVM/ASM and just interpret. Compiling things is not too easy, though you can compile to scheme to save effort (idris 2)

unkempt rock
#

oh so you mean that making high level language is easier and efficient ?

flat gazelle
#

not sure about efficient

unkempt rock
#

ohh then whats the way?

flat gazelle
#

depends on what you mean about efficient

unkempt rock
#

efficient in sense like python's maplotlib and seaborn libraries

#

it only takes a couple lines

hollow crane
#

Efficient to write normally trumps efficient to run

unkempt rock
#

but the output is big

hollow crane
#

But length of code isn't really efficiency

#

Are you talking about shortness of code or execution speed

unkempt rock
#

yeah kind of

hollow crane
#

What

#

Which one

#

This isn't even inclusive or

#

It's vaguely affirmative or

unkempt rock
#

shortness of code

hollow crane
#

That's not a great thing to optimise

flat gazelle
#

do you want to write a lang in few lines, or a lang that needs little code to do things

hollow crane
#

By all means avoid unnecessarily writing stuff people have written before

unkempt rock
#

a lang with little code

hollow crane
#

But trying to reduce character count is pointless and normally harmful

unkempt rock
#

ohh

flat gazelle
#

yeah, do not do that

unkempt rock
#

then what about making a lang like c?

wide shuttle
#

I think we've strayed a bit from the topic of this channel.

hollow crane
#

Yep

unkempt rock
#

then is there any other channel for this particular topic?

hollow crane
unkempt rock
#

ok thanks for the help i will post there

leaden lava
#

Hello fellow Pythonists! I just wanted to ask whether it'd be a good idea to create a tunnel between one of dedicated channels and Stackoverflow Python chat?

stable grail
#

as a pythondiscord server idea?

crisp wharf
#

@crisp wharf have you deployed to production before?
@north root didnt get notification, what do you mean?

stable grail
#

or as your own personal project?

crisp wharf
#

@crisp wharf By automatic testing do you mean pytest and unittest?
@unkempt rock Pytest and mocking for now

leaden lava
#

@stable grail As a pythondiscordserver idea

stable grail
#

so im personally not sure how we could moderate such a feature. but any pythondiscord ideas can be brought up in the channel for it, its called #community-meta

#

its a channel made for these kind of discussions

leaden lava
#

Thanks @stable grail I didn't know that

#

I'll ask it there

urban hollow
#

Hi. I wanna make a project with using google maps or normal maps. when i click to any country i wanna get some info about that country from the data base. basicly i will click to africa contenent and program will show what is the pop currency and more

red solar
#

what part of that requires google maps?

#

you can just draw a map with python3, have a clickable area on each country, and if you click it, it will show you the info stored in your db?

urban hollow
#

which lib i can use for this

red solar
#

any of these

#

(for the display part - you need something for your database too - probably start with sqlite3? (or json if you don't want to learn sql)

#
    def acquire(self, blocking=True, timeout=-1):
        me = get_ident()
        if self._owner == me:
            self._count += 1
            return 1
        rc = self._block.acquire(blocking, timeout)
        if rc:
            self._owner = me
            self._count = 1
        return rc

    __enter__ = acquire
#

i had no idea you could do this

wide shuttle
#

Could do what?

red solar
#

the __enter__ thing

wide shuttle
#

Think about it from another perspective, would you be surprised if you saw this at the module level?

#
def function():
    return "some return"


some_name = function
hazy anchor
#

i think 'sickened but curious' is the correct reaction.

wide shuttle
#

Yeah, it's mainly to bridge towards that this works the same within the context of a class definition

#

It's only that the eventual names end up as class attributes instead of module-level names

#

A def statement still does the same thing: it creates a function object, then assignes a name to that function object so we can use it

#

The only difference is that when that happens within the context of a class definition, assignments end up as class attributes

#
name = [1, 2, 3]

def function():
    pass

class Foo:
    name = [1, 2, 3]

    def function():
        pass
flat gazelle
#

what is quite interesting

In [14]: class O:
    ...:     def __int__(self): return 4
    ...:     as_int = int
    ...:

In [15]: O().as_int()
Out[15]: 0

In [16]: class O:
    ...:     def __int__(self): return 4
    ...:     as_int = lambda self: int(self)
    ...:

In [17]: O().as_int()
Out[17]: 4

In [18]: l = lambda s: int(s)

In [19]: class O:
    ...:     def __int__(self): return 4
    ...:     as_int = l
    ...:

In [20]: O().as_int()
Out[20]: 4
wide shuttle
#

It's because int does not have a __get__

#

So, in your first example, you don't get a "bound method"

#

You just run int() straight without arguments

#

With gives you a 0

#

It's not the definition that does anything special for "methods"; it's accessing them

#

The descriptor protocol ensures that the object gets passed as the first argument by binding it to the callable

#

That's what "creates" the bound method from the function object

#

As int does not have a __get__ method, no such binding happens and the object does not get "bound" to int

#

You just get int back directly when you access the attribute

#

In the other two cases, you bind the class attribute to a callable that does have a __get__

flat gazelle
#

interesting

wide shuttle
#

!e

def perfectly_usuable_as_method(self):
    print(self)


class Foo:
    method = perfectly_usuable_as_method

f = Foo()
f.method()
fallen slateBOT
#

@wide shuttle :white_check_mark: Your eval job has completed with return code 0.

<__main__.Foo object at 0x7f11e40af9d0>
wide shuttle
#

It's not about the place of definition of the callable

#

A def statement (and a lambda expression) just creates a function object and assigns a name to that function object

#

Whenever you assign a name in a class definition, that name just happens to be a class attribute

#

So, there's nothing special at definition time (edit: removed something from this message that had nothing to do with this to avoid confusion)

#

The binding of the object happens at attribute access time

#

It's just that a def statement "hides" the fact that these two statements are very similar:

name = "something"

def name():
    pass
#

Both create an object, and then assign a name to it

#

In R, a function definition is less appealing from a readability perspective, but does make this assignment more clear:

#
name <- function() {
    print("hello!")
}
#

Where you see <- just think = (which is also valid in R, but makes the style zealots of R scream)

#

You can even get bound methods from a function without ever assing a class attribute to a function

#

!e

def my_pseudo_method(self):
    print(self)

print("Function object:", my_pseudo_method)
bound_method = my_pseudo_method.__get__("Hello, world", str)
print("Bound method:", bound_method)
bound_method()
fallen slateBOT
#

@wide shuttle :white_check_mark: Your eval job has completed with return code 0.

001 | Function object: <function my_pseudo_method at 0x7f0244559dc0>
002 | Bound method: <bound method my_pseudo_method of 'Hello, world'>
003 | Hello, world
wide shuttle
#

(and obviously self is just the conventional name)

magic python
#

what's the stacktrace with respect to python debugging?

red solar
#

wow i started a whole discussion and missed all of it :/

wide shuttle
#

It was mostly me talking to myself

inland acorn
#

how I would do this is to make the decorator store the function in a list

#

so I recommend making it as part of a class

#

function_object.__name__

hasty thistle
#

can I ask what you're trying to achieve @unkempt rock?

inland acorn
hasty thistle
#

ah, neat

#

good luck πŸ‘Œ

red solar
#

while func(): continue or while func(): pass?

peak spoke
#

neither?

red solar
#

why not?

peak spoke
#

because it'll eat up your cpu

red solar
#

oh yeah but apart from that?

spark magnet
#

"pass", but what's your real func() do?

red solar
#

idk it turned hypothetical

#

originally was for a coroutine, but realised i should put asyncio.sleep(n) there instead

final whale
#

How is argument passing handled in py, does it make a new copy everytime?

wide shuttle
#

No, the opposite. The names get assigned directly to the arguments that are being passed

#

There's actually a great video about it on YouTube

#

This explains the basic idea behind how names and values work in Python

#

It also goes it into what happens when you pass arguments to a function

final whale
#

time to watch, thank you ! πŸ‘

bright forge
#

Is there a way to get the opposite of a slice in python? I have a list of dictionaries and I'm trying to find out how to find what the [int] would be for a specific one.

tacit hawk
wide shuttle
#

No, it means that there's no absolute reference point, so the size of the numbers you get by themselves doesn't really mean anything

#

but the difference between the numbers does. In this case, the calls are all consecutive to each other (following each other in a continuous manner)

unkempt rock
#

im trying to learn how to work with sqlite, what should i try to make? like a mini project i mean

red solar
#

try making an atomic queue with it πŸ™‚

unkempt rock
#

what is that

red solar
#

(don't actually use it for this in practice tho)

#

do you know what a queue is?

unkempt rock
#

can someone helps me? i am trying to do a welcome/auto-role commands, but them are the same here:

red solar
unkempt rock
#

like wait in queue to get in a match

red solar
#

yeah - so you have pop and push, push pushes an item onto the end of the queue, pop removes an item from the front of the queue

unkempt rock
#

what is this about

hollow crane
#

not a queue like in overwatch or something

#

well conceptually in that it's a list of things

#

but not specifically that

red solar
#

any kinda of queue lmao

#

idk how ow one works

hollow crane
#

oh i see i thought you were explaining the concept of a queue in multithreading

#

ignore me

unkempt rock
#

i dont know what are we talking about anymore

red solar
#

it's a list, you can only add on the end, and remove from the front

#

we call it a queue

unkempt rock
#

i just want to do someone with sqlite

hollow crane
#

i want to do someone too bud

#

what projects have you done before

red solar
#

lmao

unkempt rock
#

something* haha

#

ive done tons of things

#

i just wanna do like anything with sqlite to understand it a bit better and then replace my json database with sqlite for my discord bot

red solar
#

i still think an atomic queue in sqlite was a good idea πŸ˜’

#

why don't u use the project of converting your json database to sqlite to understand it better?

unkempt rock
#

u mean go right to my main goal

red solar
#

yeah might as well

#

you can always ask people to look at it, and help you along the way

unkempt rock
#

but i really dont know that much

#

tbh i know barely anything about it

#

help pls

#

bruh

#

and the error is clear

#

ur redefining what u already have defined

red solar
#

best way of learning is pushing yourself, and having someone help you 🀷

unkempt rock
#

i still kinda wanna do some mini project before

#

nothing too special

dreamy ledge
#

Bande de fdp

unkempt rock
#

and how i can do to have the welcome message and the auto-roles in the same .py file?

#

just like for example taking the input, storing it inside the database and then retrieving it and printing it out

radiant fulcrum
magic python
#

the following:

@attr.dataclass(frozen=True)
class blah:
    x: float
    y: float
x = blah(1, 2)
type(x)

returns __main__.blah, I'm wondering what would be the most typical way to document this in a doc string

hollow crane
#

document the class?

#

i don't really understand i'm afraid

magic python
#

i have a function that takes this as input

hollow crane
#

takes a blah?

magic python
#

yes

hollow crane
#

you can typehint with a custom class

#

there's a nice standard way of doing docstrings for argument types

magic python
#

what's the standard way, I'm not using mypy or anything

hollow crane
#

this is how pycharm likes it and it's fairly common

#

you can still use typehints to document the code

magic python
#

looks kinda rst ish, i tend to use similar structure

#

but I don't know what i'd write, if i have

def foo(x, y, blah_obj):
   '''
   Args:
       blah_obj( __main__.blah): blah obj with x, y values
   '''

just this i guess πŸ€”

hollow crane
#

yeah that should be fine i think

magic python
#

__main__ looked weird to me for some reason

hollow crane
#

eh just say blah

#

__main__.blah is just trying to give a nice way of describing where it is

#

like if your package is called fooblifier

#

i'd say the argument type should be given as fooblifier.Blah

#

or just Blah

magic python
#

i've never used the package like that before

#

perhaps it's similar to pd.DataFrame though

hollow crane
#

sorry?

magic python
#

putting the package name in the docstring for namespacing like that

pliant tusk
#

kinda makes me wish that fstrings worked as docstrings

#
def foo(x, y, blah_obj):
   f'''
   Args:
       blah_obj( {__name__}.blah): blah obj with x, y values
   '''```
*this doesnt work cause of how docstrings are handled internally*
#

cause then that would work

tepid dune
#

who has python advanced tutorial!

#

?

spice pecan
#

Something along the lines of

class Sample:
    __doc__ = f'''
    haha yes hello I'm a doc string
    '''
``` should work, but that's for classes, functions will just be a pain
pliant tusk
#

yea that would work for classes

magic python
#

i think matplotlib has a bunch of autogenerated stuff πŸ€”

spice pecan
#
def func(): pass
func.__doc__ = f'''My doc f-string''' # ew```
magic python
pliant tusk
#

that prob copys the docstring from Figure.text and sets it on figtext if i had to guess

spice pecan
#

Looks like it copies the docstring from Figure.text and puts it as the function's docstring

#
# Allegedly similar to this
def copy_doc(source):
    def actual_decorator(func):
        func.__doc__ = source.__doc__
        return func
    return actualy_decorator```
magic python
#

is this common? Or a mpl thing

#

mpl seems like it's a bit of a mess, i tried to copy an axis object the other week and got no where

spice pecan
#

I haven't encountered docstring.copy personally, but it sounds like something that would be common

#

I could see it being a part of functools

paper echo
#

@pliant tusk they cant work as docstrings because then you'd have to execute the function to get the docstring, which would be a huge mess

pliant tusk
#

i mean, that makes sense

peak spoke
#

@paper echo It's not really something that's defined on a lot of objects and relies on a module (excluding the copy methods on mutable sequences etc), they are documented under https://docs.python.org/3/library/copy.html although it's not in the regular method -> description format

magic python
#

@peak spoke do you know why one wouldn't be able to copy an object? I tried to copy a matplotlib.axis object and it didn't work, and i don't really understand why

#

seems that it should just be some thing in memory? Why is copying some things ok but not others πŸ€”

peak spoke
#

I don't know about the axis, but it can have data that is not copyable (doesn't make sense in an another object), depend on external connections like callbacks etc. or the behaviour is just not defined for it as it didn't seem necessary

magic python
#

i wanted to copy so that i could have functions that worked as typical functions, instead of loads of global state

#

but yeah, no idea

paper echo
#

@peak spoke yeah but "this is rare" isn't a good reason to omit a special method name from the special method name docs page

peak spoke
#

All of those methods can be accessed directly through basic python syntax or are used somewhere along the way to emulate types etc. while the interaface of the copy methods is the copy module

spark magnet
#

@magic python btw, don't use that ":param:" style of docstring. it's gross, and there's a better standard format.

magic python
#

@spark magnet πŸ˜„ I don't use that

#
"""
Args
   foo (int): thing
   bar (float): something else
"""

is what i go for

peak spoke
#

There are a few more dunders that are used by modules like that, like pickling

final whale
#

Is python handling elif and else: if: differently ?

hollow crane
#

nope, behaviour should be exactly the same, however i have no idea about execution speed

#

if you're just asking about basic behaviour

peak spoke
#

They both get parsed to the same instruction I believe

spark magnet
#

@final whale they are the same

final whale
#

thought that py would parse them differently

#

thank you Seagull, Numerlor and Nedbat πŸ‘

red solar
#

could you always access class attributes through self?

raven ridge
#

I'm not aware of any version where you couldn't.

red solar
#

i swear pycharm always used to get mad at me for it

rich wharf
#

i'd like to make an infodecorator class for people to use and me to use

#

should I try something like this

#
@InfoDecorator
class myDecorator:
  x: List[int]
  y: Optional[int]

for decorator creation

#

or more like a namedtuple

#
myDecorator = infoDecorator('myDecorator', 'x y', defaults = {'y': None})```
#

to be used like this

#
def somefunc():
  pass

print(somefunc in myDecorator) #False

@myDecorator([1, 2, 3])
def otherfunc():
  pass

print(otherfunc in myDecorator) #True
print(otherfunc.x) #[1, 2, 3]
print(otherfunc.y) #None
grave jolt
#

And why is it a decorator?

rich wharf
#

@grave jolt I figured that would be the best way to add information to functions

#

I can decorate it with the decorator and I'll still have a callable object but with extra useful properties

grave jolt
#

Well, that's very similar to a shortcut to a class creation

#

Maybe make myDecorator a class factory

#

And then check membership with isinstance, as with namedtuple

red solar
#

do submissions take a while to appear on the python mailing list? i sent an email to a list i'm subbed to, and can't see it in the archives or under my posted, and haven't received a response/confirmation :/

wide shuttle
#

Which list did you send something to?

red solar
#

python-ideas

wide shuttle
#

as far as I know, that list isn't moderated up front other than that you need to be a subscriber

#

but I'm not exactly sure

red solar
#

Hmmm... can u check if you can see my question?

wide shuttle
#

I don't know what you sent

red solar
#

It has my name on it tho (eventfd stuff is what I sent)

rich wharf
#

One of the things I really wish Python had was better anonymous functions

#

But they will probably never be added

#

There are a number of reasons for this but I would quite like the feature

#

Although I know it won't be added

#

Lambdas are unreadable and not powerful for a lot of Python tasks currently

wide shuttle
#

I think you showed me a use case for more powerful lambdas before

#

but, I haven't really seen the use of them in practice

rich wharf
#

yeah, but it's a rare one

wide shuttle
#

I'd very much prefer regular def statements for functions

rich wharf
#

I'd refer using them for event handling

wide shuttle
#

And I fear overuse and unreadable code, almost to the point of javascript

red solar
#

Considering you can define functions inside functions, are lambdas really needed for anything other than conciseness?

jolly flax
#

Isn't Lisp more oriented for Lambda expressions? (I'm not that versed, just that i knew somebody who was really into it and tried teaching me a bit)

rich wharf
#
handle.event(2, message => (
  if True:
    print(3)
))
wide shuttle
#

Which I think is the prime example of allowing for unreadable anonymous function chains

flat gazelle
#

they are nice for event handlers and that is pretty much it

rich wharf
#

And in cases where you want to group them inside objects

flat gazelle
#

and you can do that with decorators

rich wharf
#

If I wanted to make a class with a callback, anonymous functions are a good way to go

flat gazelle
#

python really does not like callbacks

rich wharf
#

I prefer anonymous callbacks

brave badger
#

iirc lambdas can't be used when scheduling callbacks for Kivy

rich wharf
#

I wish they would be added to Python but they won't be

red solar
#

Can lambdas store local variables? (Be a closure or whatever)

rich wharf
#

Not yet

#

Well actually yes, @red solar

flat gazelle
#

sort of

rich wharf
#
lambda: (
  x := 1,
  print(x)
)
#

so, yes @red solar

#

but they don't work with nonlocal or global or etc

red solar
#

What’s nonlocal? Thread local?

flat gazelle
#
a = 8
b = lambda: a
c = lambda a=a:a
a = 9
print(b(), c()) # 9 8
#

you could also use def here

rich wharf
#

@flat gazelle that's the expected usage for functions too

#

def functions

flat gazelle
#

the question was about lambdas

#

though def works the same

rich wharf
#

Can lambdas store local variables? (Be a closure or whatever)

#

this was the question

#

it wasn't about defaults and variable getting

#

But anyways, lambda power would be a useful change to me

flat gazelle
#

this is what a closure is though...

red solar
#

feels kinda cheating, storing the variable as a default param

rich wharf
#

That's true

#

@red solar There's a better and more clear way

#
lambda: (
  x := 1
)
#

But lambdas that do this aren't pythonic

flat gazelle
#

that does something entirely different

rich wharf
#

It still stores a local variable though, right?

wide shuttle
#

What’s nonlocal? Thread local?
@red solar

No, nonlocal is for names in the nearest enclosing scope that is not the global scope. You usually see it in things like decorators where you define a function within a function:

def outer():
    counter = 0
    def inner():
        nonlocal counter
        counter += 1
red solar
#

oh nice

#

but also why? just don't define it locally before you use it

flat gazelle
#

but he was asking about closures, and := is not really useful for closures

rich wharf
#

I don't interpret it that way

#

I'm interpreting Can lambdas store local variables? (Be a closure or whatever) as Can lambdas store local variables? (Through closures or some other method)

red solar
#

sorry i asked that badly, i meant through clojures but my mind had a brain freeze and i wasn't sure if clojure was the right term

flat gazelle
#

clojure is not, closure is

red solar
#

*closure lol

flat gazelle
#

there is no neater way that I know of to capture the value of an outer variable

#

Lisp and raku do that by default, but python resolves names when the function gets called

raven ridge
#

but also why? just don't define it locally before you use it
@red solar nonlocal let's you rebind a variable in an enclosing scope to a new value. Without nonlocal you can read or modify an object from an enclosing scope, but not assign to it.

wide shuttle
#

@red solar Take a look at this very naive decorator:

#

!e

def count_calls(func):
    counter = 0

    def wrapper(*args, **kwargs):
        nonlocal counter
        counter += 1
        print(f"This function has been called {counter} times")
        return func(*args, **kwargs)

    return wrapper


@count_calls
def foo():
    print("Running foo!")


@count_calls
def bar():
    print("running bar!")


foo()
foo()
foo()
bar()
bar()
foo()
fallen slateBOT
#

@wide shuttle :white_check_mark: Your eval job has completed with return code 0.

001 | This function has been called 1 times
002 | Running foo!
003 | This function has been called 2 times
004 | Running foo!
005 | This function has been called 3 times
006 | Running foo!
007 | This function has been called 1 times
008 | running bar!
009 | This function has been called 2 times
010 | running bar!
011 | This function has been called 4 times
... (truncated - too many lines)

Full output: https://paste.pythondiscord.com/ikomuwiwuy

wide shuttle
#

We don't want to redefine counter each time the wrapper/function is called, since we need to keep a count across calls

red solar
#

ahh ok i see - kinda seems like static local

#

but ok that use makes sense

#

Completely forgot variable name shadowing was a thing in python

hollow crane
#

Why doesn't counter get garbage collected there

#

What's the reasoning behind there still being a reference to it

#

Is that just how decorators work

wide shuttle
#

The inner function holds a reference to the nonlocal object in its closure

#

Otherwise, decorators couldn't work at all (if you think about it, the function that was passed in, func, also is nonlocal to the inner function)

hollow crane
#

Ahhh I see what I'm not understanding

#

Ok thanks

wide shuttle
#

!e

def outer(func):
    def inner():
        print(func)
    print(inner.__closure__)

@outer
def some_function():
    pass
fallen slateBOT
#

@wide shuttle :white_check_mark: Your eval job has completed with return code 0.

(<cell at 0x7f5b551749d0: function object at 0x7f5b55120e50>,)
shut anchor
#

I mostly use it in sorted calls.

#

Whenever you don't feel like doing a def

brave badger
#

Typically in one-off function calls that take a key parameter

radiant fulcrum
#

i'd say in any place where you are not going to use the function more and once

#

like pure says a one off

gloomy rain
#

@unkempt rock A lambda is unnamed, which implies it will only be used in one place. It can also only contain a single expression, so it should be relatively simple.

radiant fulcrum
#

its generally bad practice to assign lambda to a var and use it multiple times

gloomy rain
#

Well, it's pointless, for one thing.

#

You could just declare a regular function then.

modern night
#

and lamda syntax isn't great, functions are much more readable

radiant fulcrum
#

its good for one offs

gloomy rain
#

There are certain functions that take another function as a parameter, in order to customize its behavior.

radiant fulcrum
#

where a dedicated function is probably not worth it

gloomy rain
#

If you need to call such a function, but you don't want to declare a proper function for the parameter, you can use a lambda.

flat gazelle
#
sorted(some_list, key=lambda x: int(x.id))
radiant fulcrum
gloomy rain
#

sorted is an example of such a case

#

It's a function that sorts something, and you can customize its behavior by supplying a function to define what property it should sort by.

glad goblet
#

anyone uses vscode?

gloomy rain
#

You can declare a proper function to do that, like so: ```py
def my_sort_order(x):
return int(x.id)

sorted(some_list, key=my_sort_order)

glad goblet
#

im in this weird situation

gloomy rain
#

But that's pretty verbose.

#

This channel is for discussion, not help.

glad goblet
#

oh ok

gloomy rain
#

@unkempt rock The version @flat gazelle showed is less cumbersome to write, if you're not planning to reuse my_sort_order elsewhere.

flat gazelle
#

that is pretty much it. You can also give them more parameters for formatting purposes

gloomy rain
#

There are lots of situations where you want to embed values inside strings, and there are a bunch of different ways to do it.

flat gazelle
#
f"{user_name:20}"``` will always be at least 20 characters, padding with spaces. You can make the stuff after `:` more complex to choose a specific char for padding, choose the padding side, limit size of the string, set the precision for float, ...
gloomy rain
#
v1 = "Hello, " + name + ", how are you?"  # Cumbersome
v2 = "Hello, %s, how are you?" % name  # Better, but hard to know what %s refers to in context
v3 = f"Hello, {name}, how are you?"  # Best of both worlds
flat gazelle
#

other things you can use are

f"{str(20)!r}" == "'20'" #use repr instead of str for getting the string to embed
f'{20 + 30 = }' == '20 + 30 = 50'#include the contents within {}, then place the result
#

| is the or operator, it is set union and bitwise or as well

#

or is logical/boolean or

#

| is bitwise or

#
{1, 2} | {3, 4} == {1, 2, 3, 4}
0b1010 | 0b1100 == 0b1110
{1: 2} | {2: 3} == {1: 2, 2: 3} # 3.9 only
#

you can customize the behavior with the __or__ method

magic python
#

how does one ensure that the parameters in the docstring reflect those of the function arguments? Is this just visually? Or is there a better / more robust approach

paper echo
#

pycharm yells at me if they get out of sync πŸ˜›

#

it would be nice if sphinx also yelled at you for it

#

im not sure if theres a standalone docstring linting tool that checks for such a thing. maybe one could be built using sphinx APIs

peak spoke
#

Hopefully there aren't that many that it becomes impossible to keep track of in the docstring, but I usually only document the ones that need more beyond their name and a typehint

gloomy rain
#

@magic python This is kind of an inherent problem with relying on comments. It's hard to ensure they stay up to date, because unlike code, they aren't validated just by running the code.

#

I realize docstrings are hard to avoid. I think Pycharm inspections is probably the only tool I'm aware of.

paper echo
night coral
#

can we specify which type of variable do we want in a lambda? is it possible? like in defs

def somemethod(somestr: str, someint: int):
```?
peak spoke
#

Yes, you can typehint any name

wide shuttle
#

The lambda syntax does not allow annotations, as far as I know

#

as the : is already in use as the separator between the argument list and the expression

#

If you assign the lambda to a name, you could probably annotate that name, but then you're assigning a lambda to a name

#

and not really annotation the lambda object internally

#

Lambda
lambda's syntax does not support annotations. The syntax of lambda could be changed to support annotations, by requiring parentheses around the parameter list. However it was decided [12] not to make this change because:

crystal pebble
#

Neutered?

hasty thistle
#

they probably mean that you can do more with a function compared to a lambda

#

multiple statements etc

wide shuttle
#

They are neutered in the way that they don't carry a lot of debugging information

#

When you define a function, the function object keeps track of things like the initial name it was assigned to

#

A lambda does not do things like that

crystal pebble
#

Ah

unkempt rock
#

c = ssock.getpeercert(True)
x509 = M2Crypto.X509.load_cert_der_string(c)
publickey = x509.get_pubkey()
key = Crypto.PublicKey.RSA.importKey(publickey.as_der())

modulus = key.n
public_exponent = key.e``` How to get the modulus of certificate in Python 3?
#

The above code works for Python 2

gloomy rain
unkempt rock
#

Okay

#

I thought of this as advanced python question

gloomy rain
#

That's not what the topic says.

#

Discussion on the use cases, implementation and future of the Python programming language including PEPs, advanced language concepts, new releases, the standard library, and the overall design of the language.

unkempt rock
#

Hmmm

#

Could you please answer this issue in help-silicon?

gloomy rain
#

No, and don't advertise your help channel in other channels.

unkempt rock
#

Okay

rich wharf
#

I think we could add lambda annotations

#

lambda (x = 5: int), (y = 4: str): 5 * (x + y)

flat gazelle
#

but why?

full jay
#

Also it'd be flipped

#

(x: int = 5) for example

flat gazelle
#

you already know the type the lambda has, because you are passing to something that has a type hint

boreal umbra
#

If you implement your own version of the function type, how do you make functions with it?

#

A decorator?

flat gazelle
#

sounds reasonable

hollow crane
#

why would you ever want annotations for a lambda

boreal umbra
#

@hollow crane t y p i n g

hollow crane
#

but like

#

you don't type annotate every line of code

#

a lambda isn't really a function

wide shuttle
#

I think we could add lambda annotations
@rich wharf

It was discussed (parentheses around the entire parameter list), but ultimately rejected

flat gazelle
#

actually, is there a function that works like an identity function for functions?

brave badger
#

I'd argue that if lambdas were deliberately used in code, you could probably infer the types from what it's supposed to be modifying/calling back to in the first place

hollow crane
#

yeah that's my view, it's more like a block of a code than a function

flat gazelle
#

generally, you pass them as args, and that arg has a Callable[...] type

rich wharf
#

alright

crystal pebble
#

Do you mean like something like:

    return comp (f, g)

h = compose(print, id)```?
flat gazelle
#

no, I mean literally

def id(a): return a
``` but in the builtins somehow. Kind of like `int` and `int.conjugate` are identity functions for ints
crystal pebble
#

Looks like there is not

#

That answer is 8-years old though

flat gazelle
#

probably there is none. Shame

#

would let you make something entirely with decorators

radiant fulcrum
#

built in no

#

but doesnt the identify module have that?

raven pike
#

is c++ return void is the same as python return None ?

#

or we dont return anything at all is the equivalent?

flat gazelle
#

every function in python returns something, None by default. C++ void means the function does not have a result

#

so they are not the same thing, but you would return None (or just return) in places where you would use void in C++

raven pike
#

just "return" works? wow

flat gazelle
#

you can also do

def fun():
    print('a')
print(fun())
``` and get
a
None
printed
red solar
#

@raven pike are you coming from c++?

raven pike
#

@red solar yeah, but i was not professional πŸ™‚

#

some basic oop stuff

red solar
#

ah, imagine it more like all objects are pointers (they are in cpython), and None is just nullptr (it's not)

raven pike
#

πŸ˜„ i'm still not over pointers. i mean not seeing them makes me always think

red solar
#

in cpython everything is Py_Object* 🀷 (and all functions return Py_Object*, maybe there's some exceptions for special stuff like coroutines)

#

so you can still pretend you live in a land of pointers

wide shuttle
#

This is a good video to watch when you're coming from a different language: https://www.youtube.com/watch?v=_AEJHKGk9ns

"Speaker: Ned Batchelder

The behavior of names and values in Python can be confusing. Like many parts of Python, it has an underlying simplicity that can be hard to discern, especially if you are used to other programming languages. Here I'll explain how it all works, and pre...

β–Ά Play video
#

It explains how names and values work in Python really well

raven pike
#

doesn't __ things mean don't touch that?

#

or am i missing something

#

when looking at the preview

red solar
#

it's python you can touch everything

raven pike
#

i'm afraid πŸ˜„

flat gazelle
#

in python __name__ is a dunder

red solar
#

we have no access specifiers

flat gazelle
#

they have a special meaning when used within classes

#

feel free to touch them

#

in fact, you must touch them in order to make class fields

red solar
#

if you're coming from c++, i wouldn't worry about lack of pointers so much as lack of destructors

raven pike
#

i'm always worried that if my ram would run out πŸ˜› πŸ˜„

red solar
#

when int() takes a minimum of 24 bytes, thoughts of ram kinda fly out the window

raven pike
#

i like your avatar, did you make it yourself? @red solar

#

πŸ˜„

red solar
#

a while back another msging app introduced video profiles, and i asked a friend to send me a few looping gifs, and i chose this one πŸ™‚ (so no)

wide shuttle
#

doesn't __ things mean don't touch that?
@raven pike

I think there are two things at play here:

  • Single and double leading underscores are used to indicate private attributes, with __attribute being a stronger indication that also adds some "name mangling"
  • double leading and trailing underscores ("dunders"), like __name__ are given to names/attributes that have a special meaning in Python/the Python data model
raven pike
#

so _name_ are safe?

wide shuttle
#

A lot of those dunders allow you to implement your own classes of objects that behave much like built-ins and that can hook into many parts of the language

#

You shouldn't create your own; it has a very special meaning

#

But, you will typically implement the ones that exist

#

Like __init__ or __len__ or __repr__

flat gazelle
#

__name__ does not mean absolutely never touch this, probably compiler specific like in C/C++.

peak spoke
#

You probably shouldn't be accessing them directly most of the time because builtins or modules have ways of accessing them. The only real thing you shouldn't do is use their names for your own things or things may break if python does end up using them, and is confusing

red solar
#

in general where in c++ we have operator... or constructors, in python we have __magic__

#

so like operator+ would be __add__ (i think)

#

and while (like in c++) you can access them through magic, usually you use the corresponding operator or function

paper echo
#

Importantly dunders are not fully polymorphic like c++ operators

red solar
#

c++ operators are polymorphic?

paper echo
#

Can't you change the meaning of + depending on operands?

#

Or am I thinking of R

red solar
#

thats function overloading, not polymorphism

paper echo
#

Right

crystal pebble
#

You can do operator overloading, but I don't think you can change types

paper echo
#

In any case dunder methods on classes only dispatch on the class they're defined on

crystal pebble
#

So + for some type has to return that same type

paper echo
#

Interesting

red solar
#

ok i'm confused, king areyou talking about python or c++?

paper echo
#

Anyway thats why you have __add__ and __radd__ in Python

#

When I said polymorphism I meant multiple dispatch

red solar
#

ah yeah

#

there's no __ladd__? 😦

paper echo
#

Actually wait, multiple dispatch is for run time?

red solar
#

i think so

paper echo
#

__add__ is ladd

red solar
#

oh lol

paper echo
#

That's my point, those methods are not symmetric

#

a + b is a.__add__(b), or b.__radd__(a) if a has no __add__ method

red solar
#

huh ok we actually have multiple dispatch

paper echo
#

Finally falling back to built in +

#

Yeah, this is a hack to implement multiple dispatch on selected binary operators

wide shuttle
#

Yeah. (__add__ may also return the NotImplemented singelton to indicate that it doesn't implement it for this combination of a and b)

paper echo
#

Interesting

#

I always wondered what that was for

red solar
#

if __add__ returns NotImplemented, does the interpreter than try to run __radd__?

wide shuttle
#

yeah

#

NotImplemented

This type has a single value. There is a single object with this value. This object is accessed through the built-in name NotImplemented. Numeric methods and rich comparison methods should return this value if they do not implement the operation for the operands provided. (The interpreter will then try the reflected operation, or some other fallback, depending on the operator.) Its truth value is true.

See Implementing the arithmetic operations for more details.
#

!e

class Foo():
    def __init__(self, value):
        self.value = value

    def __add__(self, other):
        return NotImplemented

    def __repr__(self):
        return f"Foo(value={self.value})"


class Bar():
    def __init__(self, value):
        self.value = value

    def __radd__(self, other):
        return self.value + other.value

    def __repr__(self):
        return f"Bar(value={self.value})"


one = Foo(1)
two = Bar(2)

print(one + two)
fallen slateBOT
#

@wide shuttle :white_check_mark: Your eval job has completed with return code 0.

3
red solar
#

the only part i don't agree with is it being True why wouldn't it be False

#

like the name starts with Not

peak spoke
#

It is the default

#

You're most probably not going to be using the truthy value of it in any way, so there's no reason to provide a non default value

grave jolt
#

thats function overloading, not polymorphism
Isn't overloading just ad hoc polymorphism?

#

(as opposed to parametric polymorphism, which is called polymorphism in OOP)

unkempt rock
#

How would I go about creating polymorphic code and what are some uses?

crystal pebble
#

I think that has some good explanations of when and why you would want to use polymorphic code

modern frigate
#

@mental totem Do ik you?

mental totem
#

yep

#

we went to school together

#

franklin

#

lmao

#

you where the drone kid

#

@modern frigate

modern frigate
#

indeed

boreal umbra
#

I'm trying to think of the most elegant way to define how instances of a certain class are sorted.

#

but don't you basically have to implement __lt__ and __gt__ for each class?

somber halo
#

and __eq__ I believe. Seems to be a way to me.

peak spoke
#

I believe you only need the lt

boreal umbra
#

I wish there was a __key__ dunder that gets used for sorting if it's defined.

#

so I could do

#
def __key__(self):
    return (self.a, self.b, self.c)
#

etc

#

would also make it easier if you need it to work recursively.

#

so if self.a isn't a builtin type, it could use __key__ for that class if it's defined, otherwise try __le__.

languid mesa
#

ok not sure if this is the right place to put this but #python-discussion has been muted so... does anyone know a good place to learn matplotlib? i've found the docs but for python i'm not great at learning from docs cuz i'm not great at python

#

a good but relatively brief intro into the module that will help me get my feet wet before i dive into the docs would be great

peak spoke
#

That feels like abstracting a lot, implementing lt isn't horribly inconvenient and makes sense in the context

#

things like min also use the key arg in a simliar fashion so you'd cause confusion there

deft pagoda
#

theres a @total_order decorator somewhere

north root
#

functools.total_order

boreal umbra
#

I ended up making a base class for all these classes, and I'll make __key__ an abstract method and make __le__ use whatever that function returns

peak spoke
#

believe that's total_ordering, but it's unnecessary for sort

north root
#

you just have to define __lt__ i think with total_ordering, but sort only uses __lt__ anyway i believe

deft pagoda
#

yes

peak spoke
#

Just feels like complicating stuff more, and making your own dunder also isn't very nice

deft pagoda
#

why wouldn't you just define __lt__

#

what am i missing

boreal umbra
#

let me paste the code

#

!paste

#

I didn't actually make each class a subclass of the first one yet

#

I'm very quickly approaching inheritance-as-code-obfuscation territory

peak spoke
#

Your last class will just error out

boreal umbra
#

it's not done

peak spoke
#

I don't really see much use for your key as comparing stuff thruogh random types doesn't make much sense. I'm also not sure why you're changing your lists to be tuples when they're both sequences of arbitrary items

deft pagoda
#

dataclasses have a order=True kw

crystal pebble
#

@peak spoke What do you think looks cleaner?

#

(Sorry, one sec)

boreal umbra
#

@peak spoke I'm trying to make a framework for interacting with certain text data and I want people to be able to modify it to their liking

crystal pebble
#
getMatche = dimap matchPairs (bestMatch . sortMatches) validMatches
  where sortMatches  = sortBy (\mA mB -> compare (matchConf mA) (matchConf mB))
        validMatches = filter (getPredicate pMatchTest) . map buildMatch
        bestMatch m  | null m    = Nothing
                     | otherwise = Just (head m)```
vs ```haskell
getMatch = dimap matchPairs bestMatch validMatches
  where validMatches = filter (getPredicate pMatchTest) . map buildMatch
        bestMatch m  | null m    = Nothing
                     | otherwise = Just (maximum m)```
#

I think the second one looks cleaner and the added quality is worth the few extra lines you need to write

peak spoke
#

Not sure what I'm supposed to be looking at

crystal pebble
#

😦

#

Basically, I think a few extra lines of codes in order to define how to order things is a great abstraction if you plan on sorting in more than one place

#

I wanted to show you an example but that's the only one I have on hand

#

(The key thing to note here is in the top one I had make a sort function on the spot or to have a function somewhere that can be called upon. The alternative to this is make ordering a of our class so that we can just use the same sort function we use everywhere else and ignore the little details about what we're sorting over)

void sonnet
#

thoughts on imports at a lower level (not top of module; in a function/method), after sys.path has been modified? i've seen it in the wild, but it just feels dirty.

i just am in a spot where i need to clone a repo that contains a module i need to use, which may change with the commit ref i'm cloning (this is a CI/testing situation).

viral hawk
#

Sometimes you need a conditional import- is that such a crime?

#

I often want to avoid an unnecessary dependency while giving users a choice of - for instance - low-level IO library. So if you don’t pass in your β€œSPI” instance, it imports a library and sets up a default.

#

Actually used this as a crutch to do testing by using a custom IO class that emulated hardware more thoroughly than a mock object could. Have since learned more about mocking.

void sonnet
#

yeah, i've had plenty of instances of that in smaller interpreters (CircuitPython, which is actually what i'm testing). the constraints there have always been able to override my "its dirty" thoughts. just never personally used it in less-constrained environments. i've seen it a couple places in CPython projects, just not sure how wide-spread it is or how acceptable.

raven ridge
#

perhaps also to delay the cost of loading an expensive module until it's actually needed, but in my experience that's rarer than doing it to break a circular dependency.

deep storm
#

Does anyone know how to cast a python integer to a C integer? Possibly with cython?

peak spoke
#

For what use?

deep storm
#

i need to send a c int into sdl2 to change the keyboard output for android and ios

#

0 = UIKeyboardTypeDefault

#

4 = UIKeyboardTypeNumberPad

#

ect

peak spoke
#

This may be better suited to an another channel but should it do that itself? There's always ctypes for something builtin if it's necessary

deep storm
#

well the numbers come from python. i could assign them at a lower level in cython

#

but i thought it should be trivial to cast a python int as a cdef int but maybe its not

raven ridge
void sonnet
#

@raven ridge thanks. luckily, i'm not in a circular import. its really just a deferment since i have to wait until i have the "desired" version of the module.

true hollow
#

Are async/await considered keywords in Python 3?

grave jolt
#

Well, it depends on the version.

#

Before they were added, they weren't keywords.

true hollow
#

Python 3.8?

grave jolt
wide shuttle
#

They were only upgraded to keywords in 3.7, I think

true hollow
#

oooooh

#

cool

wide shuttle
#

async and await names will be softly deprecated in CPython 3.5 and 3.6. In 3.7 we will transform them to proper keywords. Making async and await proper keywords before 3.7 might make it harder for people to port their code to Python 3.

grave jolt
#

Yep

true hollow
#

damn, soo many keywords

grave jolt
#

Because if someone had named their variable await or async, making them keywords right away would be a breaking change.

true hollow
#

yeah

grave jolt
#

!e

import keyword
print(keyword.kwlist)
fallen slateBOT
#

@grave jolt :white_check_mark: Your eval job has completed with return code 0.

['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
grave jolt
#

As you can see, there are keywords!

true hollow
#

i didn't know that module exists lol

#

today I learned something new! yay

wide shuttle
#

It's pretty middle of the road as far as the number of keywords go

brave badger
#

Though if the properties of the decorator are supposed to be passed to the method, that's also different

red solar
#

TypeError: argument 1 must be , not int

#

tf do i do now

#

is this a normal error?

#

ah it's me being dumb and not properly reading documentation

#

yay it works πŸ™‚

unkempt rock
#

get gud

magic python
#

when debugging one can trigger an ipython session - within this session variables which are in memory can't be used in list comprehensions, does anyone know why that is?

hollow crane
#

what error do you get

#

like what happens

magic python
#

says that it's not found

#

if you have some list l that you can see with just l <ret>, you won't be able to do [x for x in l if x > 3] or whatever

flat gazelle
#

does l[0], [*l] and such work?

hollow crane
#

what about vars["l"]

magic junco
#

Are python lists arrays?

flat gazelle
#

In the background yes.

magic junco
#

but you can't store multiple data types in an array

#

how does python do that?

flat gazelle
#

They are all the same type, PyObject *

peak spoke
#

They're arrays of pointers to the python objects, so it's still one type

magic junco
#

does this mean that string and integers are all the same type in python?

flat gazelle
#

They are both python objects, but they do have some specific properties as well afaik

magic junco
#

I see, thank you

gloomy rain
#

So that means there's no way to store, say, an array of integers side by side in memory? I mean, without calling into some C library?

flat gazelle
#

not that I am aware of, you would need to use something like the builtin array library

red solar
#
import ctypes

ten_int_array = ctypes.c_int * 10
for i in range(len(ten_int_array)):
    ten_int_array[i] = i
#

if array is too boring πŸ™‚

hasty thistle
#

πŸ€”

flat gazelle
#

you can also use bytearray for just 1 byte unsigned ints

red solar
#

does python not have atomic types?

hollow crane
#

like what

#

it doesn't have fixed size atomic types by default

#

as in a char or an unsigned long or something

red solar
#

i don't want to use a mutex for a single int :/

hollow crane
#

for hinting?

red solar
#

no for thread safety

hollow crane
#

oh wait

#

yeah

#

seems like a common problem

#

maybe python's atomics are threadsafe

#

idk

#

or like in assignment

red solar
#

python has atomics?

#

the only thing i can think of is x = y is atomic, and then if y changes, x won't

#

(and technically it's only atomic on some platforms)

timid orbit
red solar
#

oh yay just been doing this πŸ™‚

timid orbit
#

I've also tried using a void* as struct member, but that led tho this: cpp src/py_nitro_emulator.cpp: In function β€˜int NitroEmulator_Init(PyObject_NitroEmulator*, PyObject*, PyObject*)’: src/py_nitro_emulator.cpp:8:33: error: expected type-specifier before β€˜nitro_emulator’ 8 | self->emulator = (void*)new nitro_emulator(); | ^~~~~~~~~~~~~~

red solar
#

it's header only?

timid orbit
#

it's not; I'm trying to write an extension for a Cpp lib but it uses classes

red solar
#

are you linking the cpp file? (the nitro one)

timid orbit
#

I am

red solar
#

have you defined tp_alloc, tp_new or tp_init?

timid orbit
#
# setup.py

import glob
from setuptools import setup, Extension

setup(
    name="libnitro_native",
    ext_modules=[
        Extension('libnitro_native',
                  sources=(glob.glob("src/*.c") +
                           glob.glob("src/*.cpp") +
                           glob.glob("../../src/*.cpp")),  # libnitro source; I'll set up linking later
                  include_dirs=["include/",
                                "../../include/"],  # libnitro headers
                  language='c++')
    ]
)
red solar
#

also does python allow c++ classes? even with c++ linkage?

timid orbit
#

the issue is that nitro_emulator somehow isn't found by the compiler and I have no clue why

lost nexus
#

is there any other advantages to double assignment over tuple unpacking (for assigning the same value to multiple names)? I know some compilers do this in auto-optimization and that one-less instruction doesn't really matter with modern cpus.

timid orbit
#

@lost nexus if you provide two samples I can look into what's faster and why

red solar
#

@timid orbit can you try running the command python3 setup.py build executed but with g++ instead of gcc?

timid orbit
#

I wouldn't know how to make python select g++

red solar
#

no just copy paste what it did

#

but with g++

lost nexus
#
one = one_aswell = 1
# load 1
# store one
# store one_aswell

(one, one_aswell) = (1, 1)
# load 1
# load 1
# store one
# store one_aswell
timid orbit
#

same error with g++

red solar
#

damn :/

flat gazelle
#
In [12]: dis.dis('one = one_aswell = 1')
  1           0 LOAD_CONST               0 (1)
              2 DUP_TOP
              4 STORE_NAME               0 (one)
              6 STORE_NAME               1 (one_aswell)
              8 LOAD_CONST               1 (None)
             10 RETURN_VALUE

In [13]: dis.dis('(one, one_aswell) = (1, 1)')
  1           0 LOAD_CONST               0 ((1, 1))
              2 UNPACK_SEQUENCE          2
              4 STORE_NAME               0 (one)
              6 STORE_NAME               1 (one_aswell)
              8 LOAD_CONST               1 (None)
             10 RETURN_VALUE
``` there is a difference
peak spoke
#

Well there's no need to use unpacking there where you can just store two names

flat gazelle
#

though do note that

a, b = [], []
``` and ```py
a = b = []
``` are different
lost nexus
#

i know

timid orbit
#

DUP_TOP is a lot faster than UNPACK_SEQUENCE

peak spoke
#

Python is really too high level to worry about cpu instructions (and in most applications lower level languages too)

red solar
#

c++ is fine to worry about asm

lost nexus
#

@peak spoke i know i'm just curious if there's any other advantages

flat gazelle
#

optimizing bytecode is good as in it speeds things up. But generally at that point you will be better off using FFI

lost nexus
#

FFI?

flat gazelle
#

foreign function interface, the way to call C stuff

fallen slateBOT
#

Hey @timid orbit!

It looks like you tried to attach file type(s) that we do not allow (.zip). We currently allow the following file types: .3gp, .3g2, .avi, .bmp, .gif, .h264, .jpg, .jpeg, .m4v, .mkv, .mov, .mp4, .mpeg, .mpg, .png, .tiff, .wmv, .svg, .psd, .ai, .aep, .xcf, .mp3, .wav, .ogg.

Feel free to ask in #community-meta if you think this is a mistake.

lost nexus
#

ic

timid orbit
#

:(

peak spoke
#

Just worry about what's more readable while keeping your behaviour

timid orbit
#

will dm source @red solar

lost nexus
#

what do you see as more readable

peak spoke
#

in the case of immutables the multiple assignment is probably more readable than unpacking, and if you need to do it for something longer it'll be better to just do it on separate lines

lost nexus
#

yeah

rich wharf
#

thanks @brave badger

red solar
#

is there a way to synchronize objects in shared memory? do i literally need a lock?

#

there's no lock free way?

#

ok ig moving on cuz i can't find anything online

#

if we had atomic, would it be atomic.long, atomic.Long, or atomic.AtomicLong? (ok probably can't be the first one because with atomic.int, from atomic import * would cause it to clash with int())

unkempt rock
#

thats why you dont from x import *

red solar
#

yeah but you know someone's going to do it lol

unkempt rock
#

thats their own fault IMO

red solar
#

maybe, but i'm not gonna shoot them in the foot for slightly misusing a language feature πŸ˜‚

#

so Long or AtomicLong?

unkempt rock
#

that i have no clue ;-;

red solar
#

damn :/

unkempt rock
#
from pynput.keyboard import Key, Controller
import time

keyboard = Controller()

time.sleep(2)

for char in "wwwwww":
    keyboard.press(char)
    print(char + " was pressed")
    time.sleep(0.02)

so basically this code works fine h
however when I enter a game the player doesnt move forward or whatever
how can I fix this? I made sure the code ran
pls help

true hollow
#

@unkempt rock please visit #β“ο½œhow-to-get-help πŸ™‚
this channel is to talk and discuss about PEPs, implementation of the python language, the future of the python language and etc

#

you can read more about this on this channel's topic

unkempt rock
#

oh

raven pike
#

do you guys think python should borrow series from pandas?

#

i mean it's like lists but different you know, and it might be so valuable to so many libraries and people ofc

paper echo
#

i think not

#

series isn't just a data structure

#

it comes with a huge amount of functionality

#

adding anything to the standard library at this point deserves very very careful consideration

cedar nimbus
#

where i can find a list of all global variables that python makes such as __name__?

charred wagon
#

Under the "modules" section

red solar
#

@paper echo why?

#

i wanna add atomics :/

raven pike
#

@paper echo not the functionality, the structure. just leave something like _init_ for people to customize it themselves

paper echo
#

what's the point then?

#

python already has array.array

#

what's an atomic? @red solar

raven pike
#

can arrays be appended?

#

i meant non-numeric functions, but series like functions should be implemented

red solar
#

an atomic is an object on which operations are indivisible (so thread safe)

paper echo
#

@raven pike i just dont see the point

#

other than "making python itself bigger, harder to re-implement, and harder to test/maintain"

raven pike
#

maybe i'm over-thinking

paper echo
#

when we already have well-established powerful libraries doing this stuff

#

if anything we should be deprecating old things in the stdlib like the http parser, xml parser, etc. and handing them off to 3rd parties willing to do their own maintenance. but that's a somewhat extreme opinion

#

@red solar i think that's the whole point of the GIL, so good luck with that one πŸ˜›

raven pike
#

shouldn't there be an initiative first? i mean someone should make something like these parsers; then they can hand it over.
cutting them in order to make community take care of them is sounding like a harsh solution as you put

paper echo
#

i think thats backwards

#

libraries get popular, then they get funding by some big organization like the PSF or NumFocus or Pydata

red solar
#

GIL doesn't help if i want to put my object in shared memory

paper echo
#

what language has atomics? c++?

raven pike
#

also asked in general but this place seems more appropriate for this one:
how i can retrieve a type stub file? for example for pandas, where can i get it? or everyone doing custom ones for themselves?

paper echo
#

id just be curious what an "atomics" library looks like

raven pike
#

@paper echo that's kind of scary, orgs behind these; then they might disappear one day?

red solar
#

you don't want to see c/c++'s one - you won't understand a word of it (i don't)

paper echo
#

the PSF might disappear too πŸ€·β€β™‚οΈ

red solar
#

what parsers?

raven pike
#

@paper echo 😦

paper echo
#

@raven pike you want to physically obtain the .pyi files? or just make sure that mypy can find them?

raven pike
#

actually i'm using pyright; and it can't catch lots of things from docstrings

red solar
#

thought pandas would be written in c - turns out they just use scipy/numpy :/

peak spoke
#

There's typeshed for python, but you'll have to search for other libraries if they didn't make it themselves

paper echo
#

@red solar they have some Cython code at the low level

red solar
#

where? i'm looking at the github

paper echo
#

but yes it's based on Numpy (which is mostly C extensions)

#

there are a few .pyx files in the pandas repo

raven pike
#

the official one right?

red solar
#

yeah

raven pike
#

or are there some community made ones?

paper echo
#

pandas has stub files for sure, idk where they are

raven pike
#

i downloaded a mypy extension for pandas, but it's full of false-positives

#

science-data-types i guess its name

paper echo
#

i never got an extension

#

i just use mypy and it works on pandas stuff

raven pike
#

gotcha. do you think chaining might confuse mypy @paper echo

#

like df.drop(...).dropna(...).etcetc

paper echo
#

no

#

numpy i think has stub files in the main repo

#

pandas might too somewhere

harsh delta
#

any free server to use mqtt portal

#

??

magic python
#

i've no idea if there's any reasoning to this... but in path lib there's Path().exists() and in pandas there's pd.DataFrame.empty, they seem to do pretty similar things? Why is the pathlib a method and the pandas an attribute πŸ€”

#

exists doesn't seem to take an argument, so idk why it can't just be an attribute too... it probably doesn't matter at all, i was just curious

inland acorn
#

this is just a guess, but exists is probally calling the system to check it, while empty is just checking values that are in ram. might be wrong on that tho

#

@magic python

magic python
#

right, makes sense

magic python
#

Is it possible to use a logger across different modules?

#

I guess I'd have to pass it as an argument to them... not worth it perhaps

wide shuttle
#

within the same application?

#

You shouldn't have to pass them around

#

Just use:

log = logging.getLogger(<samenamehere>)
#

but usually you want unique names that reflect the hierarchy in your project

#

a common version is

#
log = logging.getLogger(__name__)
magic python
#

@wide shuttle same application, I guess? I have a script that calls various other modules to process analysis... though maybe that's just bad design from the start πŸ€”

paper echo
#

i usually pass around loggers, idk why

#

notably if you run something with -m the __name__ changes to '__main__'

magic python
#

i've been using loguru ... i think this project is probably an unsalvagable clusterfuck at this point though

gloomy rain
#

Passing around loggers sounds like a huge hassle

#

There's a reason why they are considered to be a valid use case for singletons

magic python
#

@gloomy rain a singleton means that only one instance of it is created?

raven ridge
#

that only one instance can be created, generally.

paper echo
#

@gloomy rain i usually do from mymodule.submodule1 import logger as submodule1_logger

#

maybe thats bad style

gloomy rain
#

It's an OOP pattern where you design a class in such a way that you can't instantiate it more than once. Not really possible to implement properly in Python, since it doesn't enforce member access control.

magic python
#

so mymodule.submodule1 is the "master" script?

raven ridge
#

The standard is something like

log = logging.getLogger(__name__)
``` Anything other than that is weird, and makes it harder for users to configure logging for your libraries.
void sonnet
#

@paper echo i was doing that. gdude convinced me otherwise.

gloomy rain
#

It also essentially makes the one instance a global variable.

paper echo
#

yeah i always do that at the top of my module @raven ridge

magic python
#

@void sonnet what was the otherwise πŸ˜„

paper echo
#

i just dont typically use getlogger when i need to get another module's logger, i just import it from that module

#

(at least within an application)

magic python
#

I have

from loguru import logger as log

πŸ€”

raven ridge
#

you should never get another module's logger - I'm curious how you got into a situation where you wanted to do that in the first place πŸ˜„

paper echo
#

@raven ridge in __main__.py when i'm configuring output to the console

#

like if i run with a --debug option

peak spoke
#

Not unusual in testing

paper echo
#

that too ^ to make assertions about log output