#internals-and-peps

1 messages · Page 54 of 1

boreal umbra
#

@cloud crypt OrderedDict is still useful because __eq__ takes order into account for OrderedDict but not dict.

cloud crypt
#

eh I just need to keep track of insertion

#

basically working on new enums implementation

peak spoke
#

That seems to be more about the compile constants rather than the interning it does at runtime, none of the strings I'm handling are defined anywhere in the code and are all valid identifiers

cloud crypt
#

because current one is written in a sophisticated way and it also uses sunders (ew)

peak spoke
#

(at least the first few messages for the compile consts)

#

Nevermind I misread that, but the strings pass the content checks I know of; the only difference I can see between the strings which are interned and which aren't on the attribtues is that the first attribute exists in the aforementioned dict

clear coral
#

ah, as usual google returning old docs :|

#

@shy belfry i saw your question about having the properties with an on_change method the other day and wrote a gross hacky version that looks ahead in the bytecode to decide what to return from __get__. it works but with the usual cpython dependent caveats. let me know if youre interested in having a look.

shy belfry
#

I actually have a solution at the moment... I'm unsure what the status was back when you saw my message but I'd definitely like to chat with you about your solution! Do you mind DMing me?

clear coral
#

sure

peak spoke
#

Yeah that works when wrap the generation in the string; but have no idea how it works in the background where these strings aren't interned but the other attribute strings all are. Tried defining a dict with the strings to see if it was that affecting it but it didn't do anything

clear coral
#

did you keep a reference to the interned strings alive

#

Interned strings are not immortal; you must keep a reference to the return value of intern() around to benefit from it.

peak spoke
#

Yeah this works now by just doing my_val = sys.intern(string_generator) and they're all shared; just that my dict hypothesis was probably wrong

unkempt rock
#

hello can anyone link me any free course websites for python for beginners please?

narrow kettle
#

!resources has some links

fallen slateBOT
#
Resources

The Resources page on our website contains a list of hand-selected learning resources that we regularly recommend to both beginners and experts.

unkempt rock
#

k ty

true ridge
#

ah, as usual google returning old docs :|
@clear coral there are extensions to auto redirect and pretty useful for this kind of jobs. This is what I use docs\.python\.org\/(\d)\.\d+\/ => docs\.python\.org\/\1/

clear coral
#

which extension do you use for that?

#

also what if i WANT to visit the old docs, or beta docs, at some point

peak spoke
clear coral
#

an even bigger question: why does this actually happen? misconfiguration on the python site?

wide shuttle
#

@hearty crystal That seems like a topic for #python-discussion. This channel is meant for more abstract conversation about Python itself, from a higher-level perspective.

unkempt rock
#

How different the python virtual machine differ from the lua virtual machine?

clear coral
#

cpython vm is stack based whilst lua vm is register based

#

thats the most notable one i think

#

there used to be a register based fork of cpython

#

dunno what happened with that

unkempt rock
#

@clear coral how would a register based vm work? Don’t u still need stack frames?

clear coral
#

@unkempt rock the main difference is in how the bytecode instructions access operands. register based uses a fixed number of 'slots' to move things about and access them, whereas stack based pushes and pops things off a stack. this is a different stack to the call stack which is where stackframes would be housed.

unkempt rock
#

does anyone use type contracts when they make their functions cuz functions dont need to specify return type?

#

Functions don’t need to specify argument types either 🤨

#

Although PyContracts does look cool

steep mango
#

I got a silly question

#

f"{header_data}\n{'\n'.join(rows_data)}"

Expression fragments inside f-strings cannot include backslashes
Care anyone explain to me why?

#

it sounds very counterintuitive, as you can easily do that inside a .format() but not inside a f-string

#

(v3.8 btw)

#

Is not like it's an issue, I'm just using f"{header_data}\n" + "\n".join(rows_data). I just want to understand why f-strings don't allow that

north root
clear coral
#

the question seems to fit this channel imo

#

falls under the 'overall design of the language' purview

steep mango
#

that was my though, why python does that xD

north root
#

hm alright, i guess so

clear coral
#

probably some legacy restrictions in the parser preventing it

#

would be my first guess

#

iirc with python 3.10 there's going to be a new PEG parser so maybe this will be adressed

steep mango
clear coral
#

!pep 617

north root
#

!pep 617

fallen slateBOT
#
**PEP 617 - New PEG parser for CPython**
Status

Accepted

Python-Version

3.9

Created

24-March-2020

Type

Standards Track

#
**PEP 617 - New PEG parser for CPython**
Status

Accepted

Python-Version

3.9

Created

24-March-2020

Type

Standards Track

north root
#

lol

clear coral
#

jinx :p

steep mango
#

we 3 rock xD

tropic herald
#

use \escapes for '

#

maybe works

raven ridge
#

The PEG parser is in 3.9, not just 3.10 - though 3.9 also retains the old parser and gives a way (a -X flag IIRC) to use it instead, in case some bug is found in the PEG parser that needs to be worked around

#

3.10 plans to remove the old parser

clear coral
#

wont 3.9 be a migratory phase?

#

basically just a test bed

#

extended beta

raven ridge
#

Not from what I've heard, nor from what the PEP says

#

Starting with Python 3.9 alpha 6, include the new PEG-based parser machinery in CPython with a command-line flag and environment variable that allows switching between the new and the old parsers together with explicit APIs that allow invoking the new and the old parsers independently. At this step, all Python APIs like ast.parse and compile will use the parser set by the flags or the environment variable and the default parser will be the new PEG-based parser.

clear coral
#

so you have to use the flag to enable the old parser?

raven ridge
#

Yep, last I heard, and unless the PEP is outdated

clear coral
#

Between Python 3.9 and Python 3.10, the old parser and related code (like the "parser" module) will be kept until a new Python release happens (Python 3.10). In the meanwhile and until the old parser is removed, no new Python Grammar addition will be added that requires the PEG parser. This means that the grammar will be kept LL(1) until the old parser is removed.
In Python 3.10, remove the old parser, the command-line flag, the environment variable and the "parser" module and related code.

#

why are they keeping the old parser around

#

what benefit could there be if theyre just gonna switch to the new one as default

raven ridge
#

Like I said - to mitigate risk in case some bug in the new parser is found

#

This way someone affected by that hypothetical bug has an escape hatch, a way to revert to the old parser

clear coral
#

so... it is a migratory phase

#

testing the new parser

raven ridge
#

Well - it's not testing in the sense that users can opt in to testing a new feature ahead of schedule, nor in the sense of A/B testing. I suppose you could argue that it's testing in the sense of burn-in testing.

#

I'd say it's more accurate to say that it's being released under a feature flag than that it's being tested, since it's not as though there's any process for gathering data from that "test" other than bug reports.

#

It's a fully released feature that's enabled for everyone by default, but with an opt-out that it's expected no one will need or use

dry grove
#

For the longest time, having one of the dumbest parsers has, perhaps counter-intuitively, been such a strong point for Python.

#

I’m both excited and terrified for the future

true hollow
#

What do you think about introducing constants on python (not typing.Final)?

flat gazelle
#

What would be the merit?

true hollow
#

wdym?

#

i think it would be a good idea

#

for example, imagine you are creating a module that has some data that mustn't be modified externally, nor accidentally nor intentionally

#

a const qualifier would be a good idea

#

typing.Final can't prevent intentional modifying of a variable

brave badger
#

Immutability by convention works out just fine in most cases imo

flat gazelle
#

if someone is intentionally breaking a contract of your variable, they probably have a reason

#

SCREAMING_SNAKE_CASE already tells programmers "do not modify"

undone hare
#

Some major IDEs will warn you if you reassign those ^

true ridge
#

SCREAMING_SNAKE_CASE already tells programmers "do not modify"
@flat gazelle s/tells/yells hahahaha

true hollow
flat gazelle
#

😄

true ridge
#

The only good side I can think of such constants, is compiler optimizations, which I suspect won't be very effective. Introducing such qualifiers for default python variables would mean that every interface that interacts with them needs to rewritten for handling that conditions.

true hollow
#

consts also would be good for code protection

#

imagine overwriting accidentally a variable that mustn't be modified amegablobsweats

true ridge
#

as @brave badger and @flat gazelle said the convention just works fine.

true hollow
#

well okay

flat gazelle
#

and python generally does not let accidentally modify things. You need explicit global to be allowed to modify those

true hollow
#

i just wanted to hear your opinion peepoShrug

undone hare
#

Python decided to let the developer do whatever they want, including modifying constants, passing wrong types...

#

It has its upsides and downsides

#

I'd say the major upside is that you can wrote code faster than languages that check every operation like kotlin, and the major downside is that every operation are technically unsafe

true hollow
#

yeah

true ridge
#

Python decided to let the developer do whatever they want, including modifying constants, passing wrong types...
@undone hare and it actually brings fast development for small projects and prototypes. For big, production grade projects there are many static analysis tools that can cover the major amount of bugs normally could be inferred in other language compilers.

undone hare
#

MyPy to the rescue!

radiant fulcrum
#

I would agree tbh

#

you can build something so much quicker by just put stuff in python and prototyping compared to something like rust etc...

true hollow
#

simple prototypes in c soulcat

radiant fulcrum
#

just because you havent got to worry about any of that just to get a basic idea

true hollow
#

yeah

#

thats a big advantage of python

#

compared to another languages

#

for example c

clear coral
#

you can kind of already do constants using properties

#

for modules something like this could work maybe:

import sys
from types import ModuleType

value = 'CONSTANT_VALUE'


class Module(ModuleType):
    @property
    def value(self):
        return value

sys.modules[__name__] = Module(__name__, __doc__)
#

but i wouldnt recommend it

#

also might be possible to monkeypatch the module's __setattr__ by retrieving it using sys.modules[__name__]. though last time i tried it wasnt successful. for modules it seems __setattr__ is bypassed completely. so i must have missed something

#

ok this seems to work

import sys
from types import ModuleType

value = 'CONSTANT_VALUE_DO_NOT_CHANGE_THIS'

constants = {
    'value'
}


class ModuleProxy(ModuleType):
    def __getattr__(self, name):
        return globals()[name]

    def __setattr__(self, name, value):
        if name in constants:
            raise AttributeError('Constant attribute.')
        else:
            super().__setattr__(name, value)


sys.modules[__name__] = ModuleProxy(__name__, __doc__)
languid dagger
#

@bitter belfry This channel is not for help, and we will not spoon feed you answers

bitter belfry
#

Oke, my fault

worldly pebble
#

What is factory pattern in python?

tawdry gulch
#

Isn’t factory pattern just classes..?

#

*or using classes to construct objects

zenith topaz
#

It's using classes to construct objects yes.

wide shuttle
#

@unkempt rock This is still not the appropriate channel. We have up to 32 help channels and this project clearly involves d.py; I'd say our #discord-bots channel is on topic for that. Also, I can't find that screenshot you posted; I don't think it's from this server.

north vector
#

its faster than regular python, while being fully compatible with it (so long as its pure python)
thats its main problem, is its python written in python (just as C was written in C), so you can't use libraries that utilize C along with it

#

so some libraries won't work, if theyre not pure python

radiant fulcrum
#

PyPy is faster if your tasks are longer

north vector
#

however, for all pure python tasks, its lovely

radiant fulcrum
#

and providing its compatible which it can be relevant as things like dockerized pypy are very awkward

#

PyPy is also slow for short tasks

north vector
#

Guido, the creator of Python said if you need speed, use PyPy

#

so theres your endorsement

radiant fulcrum
#

eh sure, if you can get away with using PyPy use it by all means but it is very hit or miss with compatibility

north vector
#

yeah, not perfect. its why i still use cPython mainly, i just use too many different libraries that PyPy doesn't support

radiant fulcrum
#

mhmm

north vector
#

indeed

radiant fulcrum
#

it should be pointed out it is possible to use C libs with pypy

#

but they need to have seperate pypy distributions because of how PyPy works

#

so some libs which use C are compatible with pypy because they have a separate section for pypy

north vector
#

depends on the task IMO

peak spoke
#

It all depends on what you're doing, if it's some heavy matrix math you're better off with cpython and numpy directly

#

Then there's also numba's magic for math functions

north vector
#

a while ago when i wrote my own genetic programming framework for AI, python ran it quite slow. pypy sped it up by quite a lot
however i eventually wrote it in cython which compiles to C, and now its at peak performance

#

if its pure python, then i always prefer pypy, simple as that. but most of the usage of python isn't pure

minor sinew
#

you can also just directly use the functions from the c dll directly

#

but there is some setup involved as far as making function prototypes in python

#

nothing too complicated though

smoky ginkgo
#
def start_dynamic(case, aspen, act_doc):
    print("start_dynamic")
    """checks if integrator is running and if not, starts dynamic simulation"""
    case.AppActivate(aspen.Caption)
    case.SendKeys('{F9}', 0)
    time.sleep(1)
    if act_doc.Solver.Integrator.IsRunning:
        return None
    else:
        time.sleep(1)
        case.AppActivate(aspen.Caption)
        case.SendKeys('N',0)
        time.sleep(1)
        case.AppActivate(aspen.Caption)
        case.SendKeys('N',0)
        if act_doc.Solver.Integrator.IsRunning:
            return None
        start_dynamic(case, aspen, act_doc)

im trying to better understand what .Caption means in this instance ik the purpose of AppActivate is to take an open window and bring it into focus like say clicking on a minimized chrome window and that the application it wants to focus on is aspen but why would you need .Caption i can't find any documentation on this anywhere

#

(formatting got screwed by discord)

tacit hawk
#

Does tracemalloc shows all the memory allocated by the Python's process?

smoky ginkgo
#

doesnt seem to give me any useful information

#

ik this is some sort of win32 syntaxical stuff but idk exactly whats up

lean mica
#

Am I the only one who thinks that with open(filename, 'r') as reader: for line in reader: print(line) should return the lines without the \n in the end?

#

I remember seeing codes that does line.strip()

unkempt rock
#

i think ur escape chars r still gonna come out

peak spoke
#

You can easily parse them out, and cases where you are just changing the ontents for dumping back or processing are more likely than working on the stripped input

torpid narwhal
#

https://github.com/pyhandle/hpy

What are people's thoughts on this project? I know there was talk about maybe making the C-api easier to use and hpy being a decent alternative or maybe just pushing new ideas.

left sluice
#
with open(filename, 'r') as reader:
    for line in reader:
        print(line.strip("\n"))

You could just do this

fallen slateBOT
#

:incoming_envelope: :ok_hand: applied mute to @burnt stag until 2020-07-07 22:23 (9 minutes and 59 seconds) (reason: duplicates rule: sent 5 duplicated messages in 10s).

north root
#

!ban 730183111375061054 joined to spam chainmail

fallen slateBOT
#

:incoming_envelope: :ok_hand: applied ban to @burnt stag permanently.

ripe pier
#

is it possible to cross reference files? like I have 1 python file that imports annother python file, but this second python file needs something in a portion of the first first file. Is it possible to have python files interact that way? I would assume no, but this is based off the knowledge i have in c++

north root
#

it is not possible to have circle imports

#

one way to approach it is to have a third file with what you need to import

#

another way is to combine the two files together

ripe pier
#

okay, that was what I was thinking. Thank you f1re :D

magic python
#

how come i can do dict( zip([1,2], [3,4] ) ) instead of {a : b for a, b in zip([1,2], [3,4])}.

I get that it works, but i don't know what is happening in the former to coerce it to the latter

#

I'm not sure how I'd find this out either - presumably there's some __xxxx__ attribute or something that means this is a possibility? How would I go about answering this?

red solar
#

dict has multiple possible arguments it can take

#

one is an iterable of 2 element tuples

#

Each item in the iterable must itself be an iterable with exactly two objects. The first object of each item becomes a key in the new dictionary, and the second object the corresponding value.

boreal umbra
#

I forgot. When you make a module is the init file run first or last?

shy belfry
#

First.

north root
#

initialize

boreal umbra
#

Makes sense

#

When you import from a submodule of the module represented by the init file, can that ever cause errors? I thought it did in the past but I may have misunderstood what the error was. It was the better part of a year ago.

vivid vector
shy belfry
#

When you import from a submodule of the module represented by the init file, can that ever cause errors? I thought it did in the past but I may have misunderstood what the error was. It was the better part of a year ago.
@boreal umbra do you mean something like having submodule with __init__.py and file.py and then doing from .submodule.file import SomeClass?

boreal umbra
#

@shy belfry Yes

shy belfry
#

It shouldn't cause any issues. In fact, I tend to do that within packages instead of importing from the __init__.py since to me that's more about exposing the API to the end user / documentation framework (i.e. Sphinx).

#

Cyclic imports shouldn't be an issue either given that either way you'd be importing the same files.

boreal umbra
#

Huh. I remember deciding that I hate relative imports because pycharm didn't refractor them automatically when I moved stuff. Though it did for the ones that were absolute.

shy belfry
#

Yeah... PyCharm's refactoring stuff is kind of weird. Sometimes it works for me and other times I have to correct it on my own.

#

But it's not like that's the luxury you expect from Python.

#

Java and C# don't have cyclic imports period.

shy belfry
#

Why does this channel keep getting marked as unread?

wide shuttle
#

This channel has a very strict topic, namely discussing Python itself, from a more abstract and higher level perspective (as described by the channel topic). It's one of the only channels that we try to guard and moderate closely, as we really want to have a place in this community where advanced concepts are discussed and we have plenty of other channels to serve the needs for other types of conversation. Still, a lot of people go off topic here and those posts are then removed, leading to the unread status, as that doesn't get cleared after all new messages were removed.

drowsy mortar
#

Hi all. I'd like to know if there are any places that show how to use PYSNOW. The main site is a little hard to understand for me so far.

open trout
#

If you have googled for other places and haven't found any, odds are none of us will know any alternatives either

drowsy mortar
#

I'm still learning my way around here so I will go post over there. I was hoping someone might know something as it's sometimes a question of knowing what to search for when you start on an unfamiliar topic.

open trout
#

Ah, I see. Well, good luck!

brazen jacinth
#

Does the python compiler do any optimizations that aren't exactly peephole optimizations, or does the vm handle most if not all of the optimizations?

brave badger
#

iirc one optimization that it does is constants

peak spoke
#

I don't think there's much beyond peeophole optimizationd

#

You get immutable literals turned into constants, simper dead code it excluded etc.

boreal umbra
#

Seems like python would be hard to optimize because lots of expressions could secretly be something else.

brazen jacinth
#

yea it makes sense given everything is performed in the runtime

boreal umbra
#

The dot operator is looking something up from its hash table, except for when it's actually a function call with property.

#

Etc. I'm not an expert in this though.

#

I've wondered, if you made a subset of python where type annotations were required and everything was statically typed, would it be easier to optimize?

brazen jacinth
#

was wondering the same thing hehe, probably yes

peak spoke
#

You could partly call cython that

brazen jacinth
#

that's different because it straight up transpiles to C and then compiles it (iirc)

boreal umbra
#

Doesn't Cython only give you any benefit if your types are c types?

#

So you get zero benefit from using classes that you made?

brazen jacinth
#

easily tune readable Python code into plain C performance by adding static type declarations, also in Python syntax.

#

you're right @peak spoke

#

While Cython can compile (most) regular Python code, the generated C code usually gains major (and sometime impressive) speed improvements from optional static type declarations for both Python and C types.

#

apparently for both

smoky torrent
#

is there someone here who can help me out?

brazen jacinth
true ridge
#

Does the python compiler do any optimizations that aren't exactly peephole optimizations, or does the vm handle most if not all of the optimizations?
@brazen jacinth for the generated bytecode there are 2 steps of optimizations. When the parser outputs an AST, it gets optimized with in the Python/ast_opt.c; and after the code generation, the generated bytecode optimized in the peephole optimizer. The first step applies to you raw code, but on the other side the second step makes sure there are no redundant code out there.

unkempt rock
#

Hi. I have some questions about sort function.

#

Who can help me?

true ridge
#

The VM optimizations are mostly about stack manipulation and jumping on the right target. Like one optimization is that, branch prediction for pair instructions. When there is DICT_MERGRE, there is a high possibility of CALL_FUNCTION_EX, so python has some little optimizations to do branch pradiction.

brazen jacinth
#

Very iluminating, thanks 🙂

shy belfry
#

Wow, that's an interesting topic! The discussion on static typing made me think of Js and Ts where I don't think there are many performance benefits so I do find it interesting that in Python it possibly could have a greater impact.

brazen jacinth
#

the purpose of my mypy and TS are preety much the same are they not? static type checkers

#

well, nevermind, typescript also compiles straight down to javascript, but other then, preety much the same

shy belfry
#

Right... I know too little about MyPy to comment but there's barely a technical advantage to using Ts. Except being forced to use the right types which is less prone to errors. Lol.

brazen jacinth
#

to be honest in a language like javascript, TS is a godsend (imo)

shy belfry
#

Absolutely agreed. It forces you to be more wary.

brazen jacinth
#

what if someone just took the grammar from Python, made mypy non-optional, created a parser generator, and shoved it all down llvm's throat

#

that would be interesting

#

i guees nim is close enough without the llvm part

true ridge
#

There was seperate attempts / projects regarding LLVM+Python, but I don't remember if any of those made type checking mandatory.

cloud crypt
#

hey, isidentical :D

#

In my opinion, python is way too dynamic to fully implement mandatory typing to be honest.

brazen jacinth
#

any more notablee llvm py projects?

#

oh damn, didn't know numba was llvm

cloud crypt
#

numba is like Cython + PyPy approaching

brazen jacinth
#

Cython transpiles then compiles C code, PyPy does have a JIT compiler though just like numba

#

Then again haven't used numba much

true ridge
#

that slides also includes a simple (and very very tiny) llvm compiler for annotated python source code

brazen jacinth
#

awesome, i'll take a look

open trout
#

We talked about bools in #python-discussion and I remembered one of my pet peeves with python

#

why is '' not falsy?

brazen jacinth
#

it is

open trout
#

??

true ridge
#

indeed

open trout
#

Am I braindead?

peak spoke
#
In [13]: bool('')
Out[13]: False
open trout
#

Oh my god all this time

true ridge
#

it is a general concept, emptiness is just falsy

open trout
#

I'll excuse myself

#

4 years of python BTW

brazen jacinth
#

it would be everyone's pet peeve if an empty string was true ;P

open trout
#

yeah oh my god

#

I've been thinking of it all this time

#

I tried it once a long time ago and somehow it was truthy

#

so all this time I've worked around it and as such the truth never dawned on me

#

oh god

brazen jacinth
#

Oh my, Unalden Swallow

#

Bytecode to Intermediate llvm code

#

that's exactly what i meant

#

shame it was discontinued

#

i imagine the costs of changing to this implementation would outweigh the benefits of it, so there wasn't much interest for it

#

noone uses python for performance intensive code anyway

#

Still quite a shame

true ridge
brazen jacinth
#

Thanks, i'll give it a look

#

Most Python code at Google isn't performance critical. It's used mainly for tools and prototyping, and most user-facing applications are written in Java and C++.

#

yea probably a big factor

#

why swap the entire python runtime and implementation

#

if you can just write a microservice for performance critical sections in Rust/Go/C and be done with it

#

thanks for your slides, a nice source 🙂

unkempt rock
#

can someone explain to me what a init is im new to python

boreal umbra
unkempt rock
#

@unkempt rock ur talking about __init__ right cuz thats how u define ur constructor

#

@unkempt rock yeah

#

its how u define constructor

#

for ur class

#

that u made

#

@undone birch a possible way is to first define x as an empty array

undone birch
#

@unkempt rock Thanks!!

shy belfry
#

I was just enjoying a good night's read on how @open trout was unaware of empty string falsiness when I came over this. 😄

#

Luckily I come from Js so I can finally feel good about saying that. Where everything and their mother can be subtracted from a string.

unkempt rock
#

ya know i never really appreciated python's simplicity until i moved to c++ and i was like damn thats a lot of typing

shy belfry
#

Typing is one of the things I like about other languages though...

#

It just gives you better tooling than Python's best IDEs ever could and it's better with performance too.

#

Especially considering Python doesn't do on the fly type concersions like Js. Better just to strong-type IMO.

flat gazelle
#

I mean, quite a few languages have god awful tooling. Haskell, which is a lang with really good types has very mediocre type based autocomplete

#

same with elm

shy belfry
#

Yeah of course but I just mean I consider Python to be one of those higher level languages that so many people use and it makes it disappointing that PyCharm, a super capable IDE is limited by the language itself.

flat gazelle
#

dynamic typing in python is quite powerful, because safe, flexible polymorphism is extremely difficult. The two leading approaches are typeclasses, or java style OOP with typeclasses, but slightly more complicated (generics)

#

also, it makes it metaprogramming quite simple, and frameworks can make use of that much better than in something like java

pseudo cradle
#

I was going to say

#

How much type coercions does Python actually do?

#

I generally just get typeerrors

flat gazelle
#

Things get converted to bool

#

And under some specific circumstances to int IIRC.

#
class A:
	def __index__(self):
		return 2
 def __bool__(self):
   return False
print('Hello'[A()]) # l
print(A() or 'l') # l
#

It happens less than it does in JS

pseudo cradle
#

huh

#

interesting

drowsy canyon
#

hello everyone, I was wondering if this is the right channel to ask some questions about python's coding standards?

sacred tinsel
#

perhaps yeah, you can review the channel topic and if you feel like the question fits, you are welcome to ask here

patent isle
#

I don't know if this fits for the channel, but is it any more computationally efficient to reduce the amount of digits in a float by rounding them off if I'll be dealing with the number a lot?

raven pike
#

try it. creato two variables with rounded and float then check their memory usage

#

i guess it won't matter but I don't know

#

int vs float is a thing, but rounded may not be the case

#

i believe mypy gonna be integrated into python via a pep. Just a feeling

pseudo cradle
#

My vote is on less computationally efficient

#

Although if you're dealing with the number a lot, I recommend sympy to avoid those kinds of things

magic python
#

@pseudo cradle you'd recommend sympy for numeric rather than symbolic?

#

I mean, I've only heard it suggested for symbolic, rather than to make numerics more efficient

pseudo cradle
#

I'm making assumptions about what they're using it for/implementation, I guess

magic python
#

What are your assumptions

pseudo cradle
#

I'm assuming that they're performing multiple calculations with the same number and will thus run into compounding float errors

near coral
#

anyone using Python beta 3.9 yet?

#

or is this the wrong place to ask?

magic python
#

Interesting, so I guess using sympy could avoid floating point errors there

pseudo cradle
#

Yeah, or at least not have them compound

magic python
#

Hadn't thought of that usecase before

pseudo cradle
#

As for which is computationally more efficient, your float is already using x bits, rounding down digits doesn't necessarily reduce the amount of bits being used

#

But it probably makes little to no difference in practice

#

Actually... maybe it would always reduce the amount of bits, I'm not entirely sure and I'm kind of high so can't figure out atm

karmic fiber
#

Who made a desktop app using python and electron js

#

Anyone

north root
#

that's off-topic here

visual shadow
#

One thing to mention, reducing the bits of a number for the sake of computation sounds like premature optimization to me.

#

Revisit the actual work you're doing and see if there's any real bottlenecks present or not. Depending on what you are doing, perhaps vectorization is what you actually need, perhaps not. However, manipulating bits shouldn't really be necessary nor should it really be contributing to a bottleneck

flat gazelle
#

All floats are equally large. If you need so much precision that float math is an issue,do not use floats, use Fraction

raven ridge
#

Or possibly Decimal

nocturne wadi
#

Hey everyone. I'm new here 🙂

stuck compass
#

hey

void thorn
#

Hey Pythonite

languid dagger
#

This channel isn't for introductions.

void thorn
#

Anyone working on Chatbot?

#

It's a Good habit to say hello before question. 👍😒

charred wagon
#

This also isn't the channel to discuss chat bots. #python-discussion is more appropriate. And I agree it's nice to say hello before a question, but they never asked a question 🤔

oblique crystal
#

hey guys. I have a question/discussion that might or might not fit this channel but I don't know where it is better to be put...
I have a class with a really huge init it terms of number of args: it has n20 args. This start to be annoying for me for two reasons:

  1. almost all of them are them assigned as instance attributes so I have bunch of self.param1 = param1
  2. instantianting the class takes really lot of spcae in terms of lines, so is init (and I haven't even add yet type annotations) and, obviously the docstring might be huge (although most parameters have very descriptive names, so with type hint they won't really need a description)
#

is there any nice and or pythonic way to avoid these 2 things?

#

cause right now the class instantiaing is about 15 lines, as I pass all params as keywords rather than positional

true ridge
#

is there any nice and or pythonic way to avoid these 2 things?
@oblique crystal dataclasses can help you

#

(if you are willing to type all that parameters)

#

!d dataclasses

fallen slateBOT
oblique crystal
#

@true ridge I'll take a look. This should solve 1 right?

#

but anything on point 2? like to avoid that every time I instantiate the class writing 20 keywoards + 20 corresponding params?

#

as it looks kinda ugly 🤷‍♂️

flat gazelle
#

well, you could set default values to the args

true ridge
#
  • to the defaults, I dont know about general context of your program, but I don't advise you to write a class that takes 20 parameters. Maybe you should group 4-5 parameters into 4-5 different classes and have a class that takes the constructed ones?
oblique crystal
#

defaluts won't really help as most of those params come from DB

charred wagon
#

There can be legitimate reasons to have many args and in that case, splitting them up into dataclasses just makes things more cumbersome.

true ridge
#

@oblique crystal maybe write a classmethod, like Obj.create_from_db(db)

charred wagon
#

If you need a lot of args, then you need them, and there's no better way to do it if you want to keep them explicit and typed. If not, then simply use **kwargs.

flat gazelle
#

maybe get them as a dict and do class(**{'name': 'peter', ... })

oblique crystal
#

ok this last idea is interesting

#

so while pulling data from different tables instead of writing them to own var to write them in one param dict?

#

and then unpack it

flat gazelle
#

ye, at that point you are making a very rudimentary ORM essentially

tawdry gulch
#

Is there a dunder for dict unpacking like that?

#

I would assume not

flat gazelle
#

maybe __dict__ would work...

#

actually, probably not

oblique crystal
tawdry gulch
#

Yeah but then you’d have to do **obj.dict and you wouldn’t even have control over special behaviour

oblique crystal
#

well this works for function

flat gazelle
#

you need to be a mapping

languid dagger
#

If you want to support ** you need a keys and a __getitem__ method

#

!e

class A:
    def __iter__(self):
        print('iter')
        yield 0
        yield 1
        yield 2

    def keys(self):
        print('keys')
        return ('a', 'b', 'c')

    def __getitem__(self, key):
        print('getitem', key)
        return 'hello'

print([*A()])
print('-' * 5)
print({**A()})```
fallen slateBOT
#

@languid dagger :white_check_mark: Your eval job has completed with return code 0.

001 | iter
002 | [0, 1, 2]
003 | -----
004 | keys
005 | getitem a
006 | getitem b
007 | getitem c
008 | {'a': 'hello', 'b': 'hello', 'c': 'hello'}
brazen jacinth
#

the python interpreter eval switch case is so big, it breaks some C compilers GWmemetownOMEGALUL

languid dagger
sterile kernel
#

If you want to support ** you need a keys and a __getitem__ method
@Wattle
How come .keys() isn't a special method, if it's used internally by Python ? 🤔

languid dagger
#

You'd have to ask a Python core dev.

sterile kernel
#

@Core_devs Question for you here !

#

😄

#

I guess there is some historic reason, .keys() goes with .values() and .items(), but it's never too late to change, is it ? Someone (I) should submit the idea…

brazen jacinth
#

probably backwards compatibility

#

seems unnecesary given it doesn't add any benefit

sterile kernel
#

Well, it does, imo. Otherwise special methods wouldn't have a different pattern in naming + a conventionnal signature

languid dagger
#

You only need keys if you want to support ** for your classes which I can't see being a common case

flat gazelle
#

for example file-likes also use non dunders afaik

sterile kernel
#

Well, special methods are not about the frequency, it's about the possibility, I belive : I seldom write __getnewargs_ex__, but there it is, all the same, because it may be useful, someday

brazen jacinth
#

any promising active projects working on removing the GIL?

brazen jacinth
#

im guessing not since it's practically engraved in the implementation 🤷‍♂️

tawny shoal
#

@brazen jacinth Maybe check out

#

!pep 554

fallen slateBOT
#
**PEP 554 - Multiple Interpreters in the Stdlib**
Status

Draft

Python-Version

3.10

Created

2017-09-05

Type

Standards Track

tawny shoal
#

If I recall correctly that was the latest effort in that regard

brazen jacinth
#

😋

sturdy timber
#

You have IronPython and Jython, I think neither of those use a GIL

flat gazelle
#

removing the GIL is not difficult, neither is replacing the locks. The issue is the fact Python becomes unworkably slow as a result

brazen jacinth
#

true, but only as 2.7

tawny shoal
#

I'm pretty sure it's about as much of an issue for py3

brazen jacinth
#

i don't think the result is unworkably slow afaik

#

then again, python is not built for speed, theoretically speaking, so what if single threaded programs suffer a 30% speed hit

#

does it really matter?

flat gazelle
#

it was more like 16 times from the talk I heard on the subject

brazen jacinth
#

oof, ok fair enough

flat gazelle
#

and really, the GIL is only a problem for problems that

  1. are CPU bound and benefit from being performed 4-32 times faster
  2. would have large IPC overhead when multiprocessing
    which is extremely narrow
brazen jacinth
#

wouldn't exactly call the cpu-bound problem set narrow

#

but you're right in that regard, it doesn't really matter in py

flat gazelle
#

multiprocessing is perfectly workable

#

these days there is even shared memory AFAIK

brazen jacinth
#

meh, large overhead and ipc is a pain

flat gazelle
#

which has almost no overhead

brazen jacinth
#

shmat() maps the local process virtual memory to the shared segment. This translation has to be performed for each shared memory address and can represent a significant cost, relative to the number of shm accesses

#

no overhead, is very relative

flat gazelle
#

and well, it is probably easier to write it in java/C/haskell/pony/go/rust/kotlin/nim/... in a single thread than to try and parallelize things.

brazen jacinth
#

yea

flat gazelle
#

or use something like numba

brazen jacinth
#

like i said, not trying to argue as you're right, there exists many alternatives that are simply easier to use

#

still, the concept of the GIL interests me

tawny shoal
#

I've gotten used to using PyO3 if I ever run into situations where I need IPC

#

sometimes Kafka as well

#

or Redis

brazen jacinth
#

yea, openmp and other distributed frameworks exist or GPGPU if it's embarasingly parallel

#

redis is basically a cache, don't know if that fits well here

peak spoke
#

Processing libraries can also utilize thread parellelism in the non python code, which usually is present for anything that takes more time

tawny shoal
#

@brazen jacinth I specifically mean Redis' Pub/Sub feature

peak spoke
#

keeping locks on everything to accomodate the removal of the gil would hurt most of python's uses

brazen jacinth
#

pub/sub?

tawny shoal
#

It's fairly lightweight and gets the job done well for simple IPC

#

publish/subscribe channels

brazen jacinth
#

ahaa

tawny shoal
#

basically interprocess messaging

#

I wonder if asyncio.ProcessPoolExecutor supports shared memory

brazen jacinth
#

never used redis for anything more then a polished memcached

flat gazelle
#

channels seem to prefer it as a way to communicate across websocket connections from possibly multiple server workers

tawny shoal
#

Yeah, I personally have used it to connect different machines on the same local network that were doing different parts of a big SaaS

#

It worked well, but I did move on to Kafka at some point

#

felt more comfortable to handle the growing complexity with Kafka

brazen jacinth
#

!pep 554

fallen slateBOT
#
**PEP 554 - Multiple Interpreters in the Stdlib**
Status

Draft

Python-Version

3.10

Created

2017-09-05

Type

Standards Track

brazen jacinth
#
interp = interpreters.create()
print('before')
interp.run('print("during")')
print('after')```
#

holy guac, free isolated code?

#

this is an amazing pep

unkempt rock
peak spoke
#

Yes, assignment doesn't copy. This may be more suited to an another channel but when you do b = a you tell b to point at the exact same object a is pointing at, so modifying that object through one name will reflect when accessed through the second name

unkempt rock
#

Oh but if i do the same thing like

a=1
b=a
b=2
print(b) #returns 2
print(a) #returns 1
fathom harness
#

Hi

#

Im a new programmer

#

I just need help to fix the problem with my Fibonacci generator

true hollow
peak spoke
#

That's because you're creating a new object to be assigned to b, instead of modifying the the object that already exists. the lists would have the same behaviour you did b = a.copy(), beacuse that creates a new list with the same contents

true hollow
fathom harness
#

ok

flat gazelle
#
a=1  # label a labels object 1
b=a  # label b labels object labeled by a -> 1
b=2  # label b changed from labeling 1 to labeling 2
print(b) #returns 2
print(a) #returns 1
```vs
```py
a = [1, 2, 3] # a labels a list object
b = a # label b labels object labeled by a -> the same list
b.remove(1) # remove the element 1 from the list object labeled by b, which is the same list object as the one under a
tawny shoal
#

Is this as inteneded?
@unkempt rock not sure if it was mentioned but a lot of classes implement a copy method. list has one, for instance

unkempt rock
#

@unkempt rock not sure if it was mentioned but a lot of classes implement a copy method. list has one, for instance
@tawny shoal oh thx

tawny shoal
#

but yeah whether to create a new pointer to point to the original variable or to copy it instead is a consideration to always keep in mind

#

The lack of pointers as an explicit concept in Python can unfortunately lead to unexpected behavior for beginners

#

especially since it's different for object and object subclasses (/ "custom" classes) compared to other builtins (with some exceptions)

tawny shoal
buoyant skiff
#

how to convert from [[10,20,30,40,50]] to [10,20,30,40,50]

full jay
#

@buoyant skiff

wrapped_list = [[10,20,30,40,50]]
inner_list = wrapped_list[0]

Although next time you might want to either ask in #python-discussion or using our help system #❓|how-to-get-help. This channel is for more detailed discussions about the future of the Python language and more complex topics

brazen jacinth
#

kite is nice at first glance, but it's a real resource hog

#

afaik, maybe that's changed

full jay
#

Yeah I remember trying Kite when it first came around since @gray mirage was going on and on about it at the time. I didn't really care for it, and it didn't feel like it had many advantages over current intellisense tech

gray mirage
#

I don't feel like it adds anything over what PyCharm provides when it comes to completion

full jay
#

Do you still use it, g?

gray mirage
#

It's handy to have the docs for what you're using always visible though

#

Nope

full jay
#

That's fair. Although most of the time I'd prefer to have the websites themselves open, since I'm more than likely going to need to look up additional stuff

gray mirage
#

That's often useful too yeah

karmic slate
#

Best graphics library for python?

karmic slate
#

Like gui not pure graphics

tawny shoal
#

Depends on personal preference and what you intend to do with it, this is not the right channel for that though, try #python-discussion

karmic slate
#

I did try in there but it was way off topic then the admin muted the channel

shy vine
#

That doesn't mean randomly post in other channels

prime charm
#

[issubclass(bool, int)]

magic python
#

"f strings are almost always a bad idea' 🤔

narrow kettle
#

wut

magic python
#

i just bumped into this whilst getting annoyed by W1203

narrow kettle
#

As @chdsbd said, string interpolation is happening at run-time in a way that can lead to exceptions. And while it's abundantly clear what the potential side effects of log.debug("%s", a.thing()), it's less clear that log.debug(f"{a.thing()}") is the same thing.

#

i dont understand what this is saying

#

can someone explain what im missing here

magic python
#

idk but i'm getting E1205: Too many arguments for logging format string instead now

#

so it's pissing me off too 😄

fair mesa
#

hey just a quick broad question, how many loops can be nested in other loops?

#

like how many layers down of loops would I be allowed to go before python goes 'nah son'

boreal umbra
#

@fair mesa I'm not aware of a limit imposed by the language

fair mesa
#

ah, nice

#

well hopefully that holds true lol

boreal umbra
#

But if you are going that many levels deep, you might want to take a step back and look at your design.

fair mesa
#

it's about 4 layers deep

peak spoke
#

They should be limited to around when the 80 char limit becomes not viable (even with a higher line limit used in the project)

fair mesa
#

it's basically getting data inside of a list, inside of another list, and inside of another list.

boreal umbra
#

Recursion might make this easier to read.

unkempt rock
#

oh lol ive done sth similar with big tables and ur gonna experience some slowness

fair mesa
#

oh yea I know, and I'm gonna have to implement some pauses in it because I'm getting the tables from websites, so at minimum I'm expecting the program to take like, 25 minutes to run

#

it's pulling a bunch of reports from what's basically every single county in the US on this site, and I've counted every single county that has data and it's like 1500. And each of those counties has at least 1 report to track, so double that number. Basically, in order for me to not flood the site with html requests I gotta put pauses in between each request. And even at a second per request that's like, a lot of minutes.

#

so just because of trying to prevent accidentally ddosing I already know the code is gonna take a while to run, regardless of how many loops I use lol.

brazen jacinth
#

Ur scraping or calling an api?

fair mesa
#

scraping

brazen jacinth
#

And each country has its own page?

fair mesa
#

yea, and each county has its own reports in the county that also have their own page.

#

it's an older website, and I think it's meant to be more of an archive than anything active

peak spoke
#

got SyntaxError: too many statically nested blocks after 20 indent blocks

fair mesa
#

huh, so 20 is my limit

brazen jacinth
#

If youve got 20 nested loops you are doing something wrong

fair mesa
#

least I don't come close to that

#

noo, I would have 1 loop and 3 nested loops inside it max.

brazen jacinth
#

.thats fine

peak spoke
#

I assume it's restricted because of how the blocks are then executed, but the real limit is the limit where readability starts getting worse

fair mesa
#

and the code isn't so big to where it's hard to track, it's basically just repeating the same function on the site within the last site.

#

so scraping first site to get all links, going into all those links to scrape links from each link, and then within next one, and then last one has data.

#

so 4 layers

#

states > Counties > Reports > data

#

And I'm doing this this way, because doing this manually would've taken me like a week and a half. I even timed myself trying to speed through about 3 or 4 states worth of data, and took me 2 whole days

#

so I've been learning python in the last 3 days now just to do this thing, and I think I'm only a day or two from completing it

#

but even if it takes me a while learning it, once I've learned it, if I do something similar in the future it would take even less time, because I wouldn't have to learn it again.

oblique oxide
#

what's the best place to learn python, assuming no programming knowledge at all?

charred wagon
oblique oxide
#

oh my mistake, sorry

livid herald
#

hi, what channel do i get help in? sorry im new

tawny shoal
#

@livid herald

swift imp
#

I bet there's a nested list limit

#

I know it Matlab I've clocked it at like 126 nested for loops

#

I bet other languages have similar limitations

#

Oh missed Numerlors response

red solar
#

you could compile python3 yourself and change the limit 😉

#

perfect recipe to end up with code that only works for you lol

pseudo cradle
#

Oh god

#

I had FIVE nested loops in a complex algo and I felt bad as hell

#

like I was a dirty rotten developer of the worst kind

#

Iirc python recursion depth limit is relatively shallow but you can extend it without too much trouble

grave jolt
#

I think you can go up to ~50000 or so without getting a segmentation fault. But at that point, it'd really be better to rewrite your solution without using recursion.

pseudo cradle
#

I had a program that extended the recursion depth for a specific algorithm. I'm going to double check what I did

red solar
#

sys.setrecursionlimit(n)?

pseudo cradle
#

Pretty sure that's wht it was, lol

red solar
#

why tho, that just seems like a bad idea 😂

pseudo cradle
#

I'm just curious as to what I set it to and why

#

sys.setrecursionlimit(10**7) # max depth of recursion

grave jolt
#

Well, you can't set it to any amount you want.

pseudo cradle
#

Looks like I was doing some tree traversal and an iterative solution would have been more complex

grave jolt
#

I mean, you can, but it will just give you a segfault.

pseudo cradle
#

ah

red solar
grave jolt
#

!e

import sys
sys.setrecursionlimit(10**7)
def f(): f()
f()
fallen slateBOT
#

@grave jolt :warning: Your eval job has completed with return code 139 (SIGSEGV).

[No output]
pseudo cradle
#

F

#

rofl

#

What is it by default, anyway?

grave jolt
#

1000

pseudo cradle
#

Ah, gotcha

grave jolt
#

Well, you can emulate a recursive solution by maintaining your own call stack.

pseudo cradle
#

haha fair

#

Here's what I don't get

#

Why does a python compiler let you redefine a function in the same file?

red solar
#

why not?

#

i mean it's just an object that implements __call__ or whatever

boreal umbra
#

I don't really get what collections.ChainMap is for

charred wagon
#

It's a way to merge dictionaries

#

Yes, you can do it with unpacking into a dict literal. This is just an alternative

#

That's how I see it anyway

#

I've never used it for its dynamic feature

boreal umbra
#

won't we also be getting | for dicts anyway?

charred wagon
#

I don't know

#

They would not remove this if the pipe got added

#

Breaking change. And it's not quite the same thing

lost nexus
#

would it be preferred to use ChainMap over d |= (d1 | d2)

fair mesa
#

hey quick question, is there another way of doing large commenting out of stuff other than the # for each line?

lost nexus
#

a docstring

fair mesa
#

ah nevermind I figured itout

storm elk
#

Question: Is it allowed to call a class method as a "static" method by manually passing in a class instance as self? i.e. collections.OrderedDict.get({1: 'a', 2: 'b'}, 1) instead of collections.OrderedDict({1: 'a', 2: 'b'}).get(1)

#

It absolutely works, both with user-defined and built in classes, but I'm wondering if it's "supposed" to work or just an unintended use case.

north root
#

normal methods take self as the first parameter

#

if you don't instantiate the class, that means this is no self, so you have to pass it yourself

#

there's no black magic 😄

storm elk
#

Okay, that's what I figured, just wasn't sure if it was UB or not

#

Thanks!

north root
#

no problem!

mighty hound
#

Hey there , I am new to python 🐍

unkempt rock
#

hi

mighty hound
#

I use vscode editor with jupyter extension

unkempt rock
#

cool

mighty hound
#

Just started a week ago ,,I am taking python course from Udemy from Jose

north root
unkempt rock
#

is it a free one?

mighty hound
#

Ok @north root no problem

#

Can I post my language related questions here?

unkempt rock
#

im assuming so

mighty hound
#

Ok

raven ridge
#

This channel is more for advanced topics about the Python language itself, as opposed to getting help or learning the basics

pseudo cradle
#

@red solar Because if you're redefining a function in the same file the first one just gets completely replaced

#

Idk, imo it shouldn't even compile

#

It makes sense in C when you're creating a template/overloading a function

unkempt rock
#

c, templates... 🤔

#

Actually I shouldn’t say that, someone’s probably implemented templates with macros

brazen quarry
#

Does it make any difference to use self.attr = attr vs self._attr = attr in the __init__ when @property is implemented?

#

Because both seem to work just fine

wide shuttle
#

It does not make a difference, but _ is an indicator that the attribute itself is not part of the public API of the object and that you should access it using the property

#

A common pattern is to give the property the name you'd normally use for the attribute (e.g. def attribute) and the internal attribute it's managing access to the same name with a leading slash.

mighty hound
#

In this pic I have two functions and I want to call them inside another third function .

shy belfry
#

The same way you would call any other function, shuffle_list().

mighty hound
raven ridge
#

Why does a python compiler let you redefine a function in the same file?
@pseudo cradle because functions aren't special, they're variables like any other. def foo() is not fundamentally different from a foo = ... assignment. Functions are allowed to be assigned to multiple times for the same reason as any other variable can be assigned multiple times. This is how decorators work, after all - a decorator is syntactic sugar for

def foo():
    ...
foo = some_decorator(foo)
pseudo cradle
#

Looks like something I'll need to read up on. I've never really come across it in C++.

raven ridge
#

In Python's object model, everything is an object, including functions and classes. It would be strange and inconsistent if the variable created by a def or class statement couldn't be assigned a new value. That would require creating a whole new type of variable that behaves differently from all the other variables. It would make it so that whether assignment succeeds is conditional on whether the variable being assigned to already exists and refers to a function. It would really have horrific implications for the object model.

tawdry gulch
#

Yeah, everything is first class iirc

#

Which is cool because you can do stuff like:

def make_class(ctor, string, bleh):
  class Ret:
    __init__ = ctor
    __str__ = string
    bleh = bleh
  return Ret```
open trout
#

Ok people, docstrings. reST or Google? I wanna hear your preferences.

shy belfry
#

Ok people, docstrings. reST or Google? I wanna hear your preferences.
@open trout NumPyDoc so essentially reST.

#

I want to use Google but I prefer doing this:

name: type
  description```
#

Vs this:python :type name: description

#

Also the Sphinx setup is easy AF.

#

@pseudo cradle because functions aren't special, they're variables like any other. def foo() is not fundamentally different from a foo = ... assignment. Functions are allowed to be assigned to multiple times for the same reason as any other variable can be assigned multiple times. This is how decorators work, after all - a decorator is syntactic sugar for

def foo():
    ...
foo = some_decorator(foo)

@raven ridge technically you could even make class Foo and assign the __call__ method to recreate what you were saying.

#

Which is cool because you can do stuff like:

def make_class(ctor, string, bleh):
  class Ret:
    __init__ = ctor
    __str__ = string
    bleh = bleh
  return Ret```

@tawdry gulch or do stupid shit like ReactivePy: https://github.com/Dan6erbond/ReactivePy

pseudo cradle
#

That's so bonkers

#

That doesn't exist in C, I'm pretty sure

#

Or if it does, I have 0 idea how to implement it

tawdry gulch
#

C isn't even object orientated though

#

can't really compare

#

Personally I prefer a structural form of programming, but thats just me

wide shuttle
#

The most important point is that "functions" are just objects in Python and you can pass them around, create them on the fly, and return them from functions.

tawdry gulch
#

^

wide shuttle
#

The def statement "hides" it a little bit, but in essence:

def some_name():
    # some function body

just does:

some_name = Function(# some way to define parameters and function body)
#

This more obvious in the R programming language:

some_name <- function() {
    # function body
}
#

You may replace <- with = (it's even valid in R, just not the generally accepted style)

gritty sequoia
#

That is a great point, I always thought that made so much more sense intuitively, how that syntax was used in R.

wide shuttle
#

I think def statements increase readability, but they do hide this a bit

undone hare
#

You could do the same as in R with Python using lambdas

some_name = lambda: ... ```This is actually pretty close to what the compiler does, as ves said
wide shuttle
#

There's (at least) one thing that's different

gritty sequoia
#

I agree with that as well, Nim has an interesting take on that syntax and calls them procs, which is more in line with the contrast between pure functions or functional languages mathematical strict definition and procs that more so are just clusters of procedural code.

wide shuttle
#

Normally, when you do something like this:

name = SomeClass()

When you construct SomeClass, you don't have access to name. If the name is a class attribute, the __set_name__ hook will be ran during the assignment process, but, in general, you don't have access to it during the object construction process.

#

When you're defining a function using the def statement, that name is available (and actually stored with the object)

#

That's why the repr of a function object shows the original name that was assigned to the function object

undone hare
#

That's why we call functions created through lambdas "anonymous functions", because they don't have a name

gritty sequoia
#

That is another good point about that as well. Also, further justification of the object nature of functions, although you would probably not maybe use this in reality, is the ability to define attributes on them as well so something like func.a = "spam" isn't going to throw an error.

dense wraith
#

hello every one, I am using linsolve to solve an system of linear equation, If it has infinitive solutions, the function return a set of symbols, how can I get the independent variable and assign it a value to get all value of dependent of the other variable? My english is not good, sr

true hollow
#

please read the topic of this channel

undone hare
worldly venture
#

saw this on python-ideas

#
class _Generic(object):
    Specializations = []

    @classmethod
    def __getitem__(cls, *args):
        name = f"Generic_{len(cls.Specializations)}"
        Specialized = type(name, (cls,), {"specials": tuple(args)})
        cls.Specializations.append(Specialized)
        return Specialized

    def __init__(self, value = None):
        self.value = value

    def __str__(self):
        if hasattr(self, "specials"):
            return(f"[{type(self)} - " + ",".join(str(special) for special in self.specials) + f"] - {self.value}")
        else:
            return(f"[{type(self)} - GENERIC" + f"] - {self.value}")

Generic = _Generic()

#g = Generic() - fails because of no specialization is given
s1 = Generic[12]()
s2 = Generic[42]("Hi!")

print(s1)
print(s2)
#

kinda cool example

#

also saw a proposal for a clamp built-in which is a nice idea

#
val = 100
>>> clamp(10, val, 50)
50
>>> val = 3
>>> clamp(10, val, 50)
10
>>> val = 25
>>> clamp(10, val, 50)
25
shy belfry
#

Oh yeah I liked the clamp suggestion.

#

When you're defining a function using the def statement, that name is available (and actually stored with the object)
@wide shuttle in Js everything is the opposite way, you have classes that are just syntactic sugar for functions with state, but the syntax you mentioned in R reminds me of the () => {} arrow function.

gritty sequoia
#

JS "classes" are interesting, you are right @shy belfry. Essentially, they are really just constructor functions under the hood, and then methods are loaded up into their prototypes and linked into these prototype-based chains of inheritance. I honestly think that is the most beautiful aspect of all languages, and sort of their approaches to that fundamental question of how similarities should be encapsulated, to what those features are owned by, and then how does one share such features to other similar constructs, whether that sharing comes via extending or implementing, inheritance or mixin-based approaches, or via composition of smaller functions to produce patterns of repeatability, but yet still be able to maintain some sort of integrity to their own data and characteristics, either encapsulating it in classes or tucking it from outer scopes inside inner execution contexts which may take advantage of a closure. Eek, I think it's time for bed, I've been writing code too long and I'm getting whimsically grandiose ...cavedude LOL

brazen jacinth
#

if you need it, meh

>>> def clamp(val, lower, upper):
...     return max(val, lower) if val < lower else min(val, upper)
#

then again, if we have max and min, why not

#

5head solution

>>> def clamp(val, lower, upper):
...     return max(min(upper, val), val)
peak spoke
#

I prefer the normal if solution, which I think is more readable and should be faster

#

Wouldn't hurt to have it in the stdlib with all the edge cases taken care of, but not sure if it's worth it as a builtin

brazen jacinth
#

is it really faster though 😉

#
dis.dis(clamp)
  2           0 LOAD_GLOBAL              0 (max)
              2 LOAD_GLOBAL              1 (min)
              4 LOAD_FAST                2 (upper)
              6 LOAD_FAST                0 (val)
              8 CALL_FUNCTION            2
             10 LOAD_FAST                0 (val)
             12 CALL_FUNCTION            2
             14 RETURN_VALUE
>>> def clamp(val, lower, upper):
...     return max(val, lower) if val < lower else min(val, upper)
... 
>>> dis.dis(clamp)
  2           0 LOAD_FAST                0 (val)
              2 LOAD_FAST                1 (lower)
              4 COMPARE_OP               0 (<)
              6 POP_JUMP_IF_FALSE       18
              8 LOAD_GLOBAL              0 (max)
             10 LOAD_FAST                0 (val)
             12 LOAD_FAST                1 (lower)
             14 CALL_FUNCTION            2
             16 RETURN_VALUE
        >>   18 LOAD_GLOBAL              1 (min)
             20 LOAD_FAST                0 (val)
             22 LOAD_FAST                2 (upper)
             24 CALL_FUNCTION            2
             26 RETURN_VALUE
>>> 
#

(probs is)

peak spoke
#
    def clamp_value(value: float, min_: float, max_: float) -> float:
        """Keep `value` between `min_` and `max_` inclusive."""
        if value < min_:
            return min_
        if value > max_:
            return max_
        return value

rather than using the builtin functions

#

the builtins have some overhead because picking out from 2 values is not really their purpose, and then there's the function call which takes up some time

deft pagoda
#

i use the max(min(... construction all the time, i think it's perfectly readable, if only because i read it a lot

unkempt rock
#

I just learned about the "binary tree" data structure through pypi package anytree. Are there other data structures I should not be neglecting to learn in python?

#

Those that are not in the basic crash course.

peak spoke
#

You can take a look at collections which has some, a deque for example can come in handy

unkempt rock
#

Im familiar with most of those.

terse pivot
#

@unkempt rock Queues and stacks are required when ur using threads

#

and hashing for data storage

flat gazelle
#

finger trees are amazing

#

though in python you can mostly use lists/deque for the same purpose

slim island
#

Graphs and the different ways of representing them don't have quite so many practical uses, but are worth learning about @unkempt rock

swift imp
#

Has anyone ever used descriptors to restrict the value of an input argument to a class init or function ?

unkempt rock
#

Hello everyone.
Are you also feel frustrated with Python's idiotic errors such as
"can't multiply sequence by non-int of type 'float'"

wide shuttle
#

Having programmed in a lot of languages, I must say that I really like Python's error messages

#

This tells you exactly what's up. You've got some kind of sequence and you're trying to multiply it using a float, which isn't possible

unkempt rock
#

I'm not a programmer. Just a guy from academia. But sometimes I feel I'm lost somewhere. I need to use Spyder. I used a regex to read an analysis file. Somehow Python assumes a group of numbers as it's a list rather than arrays.

#

I hate lists. I don't need them. All I want to use Numpy arrays of floating numbers.

wide shuttle
#

I'm just a guy from academida as well, but I think you're more running into that you're still learning the fundamentals of Python

#

Python isn't just a tool for doing analyses and/or data science, which means that the language isn't designed around it as something like R is

#

list is one of the built-in data types in Python and the syntax for a list literal is [1, 2, 3]. You can use numpy's ndarray, but you have to make sure that you're using it. It's not one of the built-in types (although it's a very popular type provided by a third-party package).

#

Still, there's no issue in using them; it's just that the default [1, 2, 3] in Python is a list

unkempt rock
#

Pythons error messages are bae until theyre coming from something like SQL Alchemy.

#

A syntax error is my favorite error to get. It means that I will see the problem fairly quickly.

#

Probably my lead favorite is... "x thing cannot be indexed" or "x thing is not hashable"

#

Usually from long lists (not python lists) of nested deep json

wide shuttle
#

One you deserialize JSON-formatted text, you are working with Python lists and dictionaries

unkempt rock
#

yeah, but sometimes whoever structured the data chose to do so in a way that requires a crypting sequence of random keys and indexes to get at

#

lol

#

its not difficult but irritating

#

@wide shuttle After your first comment I realized I was dealing with list instead of numpy arrays.

hazy anchor
#

is that actually true.

peak spoke
#

python is python, but numpy is mostly a C module

wide shuttle
#

Python has array.array and numpy ndarrays are not Python lists

#

It's just that if you use the syntax [1, 2, 3], you get a Python list, not a numpy.ndarray

unkempt rock
#

@unkempt rock Wow. It is weird. I assumed/guessed we have numpy arrays to calculate fast.

wide shuttle
#

No, that's not true

#

Numpy arrays are laid out as arrays in memory

#

They're not slight modifications of Python lists

red solar
#

laid out as arrays in memory, as opposed to contiguous python lists which aren't? 🤔

wide shuttle
#

Yeah, I haven't expressed myself that clearly

peak spoke
#

numpy uses normal homogenous arrays most of the time, unlike python lists which will contain pointers at the actual objects giving any operation overhead. Then there are optimizations for multi dimensional arrays

unkempt rock
#

I figured out: numpy.asarray(mylist) solves problem.

wide shuttle
#

no, you'd typically use regular Python types when defining a user-defined class (which is called composition)

#

But, if you dive down into the C-API, you can do a lot more

peak spoke
#

you could imitate some of the behaviour with the aforementioned array.array

wide shuttle
#

I'm simultaneously trying to find the relevant parts of the numpy source code

red solar
#

oh no :/

peak spoke
#

But numpy also keeps things like iteration in C exposed to python through methods, allowing it to be a lot more performant

hazy anchor
#

see you next week fella

wide shuttle
#

It's not actually that bad; there's just a lot of it

#

But, basically, instead of using Python lists, numpy creates its own type of objects to interact with

#

And by laying out the data in memory in a specific way, you can used optimized procedures in a way that's not possible with regular Python lists

swift imp
#

How hard is it to write C level stuff and make python bindings for it?

unkempt rock
#

Depends what you want to do

#

Generally not that hard

#

More tedious

terse pivot
#

@swift imp it's easy but u need to know how both languages communicate like wrappers

pseudo cradle
#

I need more practice with it, seems like an incredibly useful tool

swift imp
#

I find it quite amazing that we can use a compiled language like that in python

radiant fulcrum
#

In what cases should we use abc.ABC based classes?

swift imp
#

When u r making an api

#

Like pandas allows array extensions, and they have an ABC class

#

It has methods and properties that they require you to define it else it errors

burnt wind
#

I'm trying to understand the importing stuff

#

django.http

#

it finds the django files and .http calls the class of http and then you import HttpResponse from it right?

#

I do know it had something to do with classes though (at least I think I do know)

flat gazelle
#

django is a package, http is a module, HttpResponse is a class in the http module

burnt wind
#

ah gotcha

#

thanks

burnt wind
#

anyone know how to deactivate a venv?

#

the deactivate doesn't work

brazen quarry
#

deactivate always work for me

#

maybe your venv has a bug or virus

burnt wind
#

This is what i'm doing

#

It still stays the same

#

there isn't a .ps1

slim island
#

Just type deactivate

#

This isn't the channel for it though

burnt wind
#

idk where to get help

#

It's the virtualevn module

#

I thought it counted lol

slim island
burnt wind
#

well I didn't wanna take up 20 mins for a question that can be answered easily

brave badger
#

Again, this is not the appropriate channel to get help from.

wide shuttle
#

Hey, @burnt wind, I appreciate that, but you don't really have to worry about taking up a channel. Our help channel system dynamically scales up to a max of 32 channels and we rarely get close to that. For quick questions, #python-discussion is the channel you'd want; this channel is specifically a discussion channel to talk about Python itself from a higher level and more abstract perspective. It's not a help channel in that sense, but we have up to 32 channels that are help channels.

burnt wind
#

ah alright got it

formal musk
#

Hey guys

#

I’m a python developer specializing in Django, machine learning, deep learning and cyber security

sullen widget
#

sup, u do any ctfs?

coral relic
#

@sullen widget what about changing your profile pic ?. i think you should respect others religion!

sullen widget
#

it's an image.

#

there is nothing hate speech related or anything like that

coral relic
#

but with that image there is no respect i think. i know you are free to put any image. but if you are muslim. i think you will be so sad when you see this.

sacred tinsel
#

Let's please try to keep this channel on-topic

coral relic
#

okay sorry for saying this guyd

#

s

#

as you like already i'm leaving ♥

sacred tinsel
#

if you'd like to speak to our moderators, feel free to send a DM to @summer lichen

sullen widget
#

agreed

crisp prawn
#

But, if you dive down into the C-API, you can do a lot more
@wide shuttle I have a question, generally, I am new here, and I am looking to include into my tech stack -the C programming language, am I in the right path with learning python first, then C later down the line? Should I explore other languages like C++?

#

or C#?

terse pivot
#

python and then if u need like super optimized code learn C++

astral lark
#

Hello can anyone introduce me with namespaces in python ?

terse pivot
#

C++ is used when python is compiled and if u likkme need more control over that process u can

astral lark
#

i am new here

terse pivot
#

be specific

astral lark
#

i want to know just the topic namespaces in python ?

#

module namespaces concept ?

terse pivot
#

module.function()

#

something like this

true hollow
#

@astral lark I think you should go to a help channel (read #❓|how-to-get-help)

This channel is, as the topic says, for discussing PEPs, the Python language and the future of this language

lime dagger
#

I am giving a interesting python project challenge. Would all of you like to solve it?

#

Type "yes" if you want to

terse pivot
#

yes

true hollow
#

I think you should contact the admins or owners if you plan to host a user event here @lime dagger

#

cuz this is not the appropriate channel
idk

lime dagger
#

No, by typing only i am giving the challenge

#

I will give you the input, output every details

#

Its just a challenge for refereshment

#

After some days, I will host a event and there i will discuss the answers

boreal umbra
#

@lime dagger I would suggest taking this to another channel

shy belfry
#

Who else here has a love-hate relationship with descriptor protocol?

wide shuttle
#

I mostly have a love-love relationship with it

shy belfry
#

Well, I just had to implement a proxy so that I could use the descriptor protocol magic for instance attributes and it feels weird. Also having to take care of edge-cases by implementing __call__ and extension_func() which just call the main class' members.

unkempt rock
#

hey I needed some help with creating a .py file into an exe could anyone help?

shy belfry
unkempt rock
#

when I tried to do it

thick bay
#

Any good resources to learn Python? I’m new to programming

unkempt rock
#

Can anyone help when I try to convert my .py file into a .exe file it comes up with this error Fatal error in launcher: Unable to create process using '"c:\users\user\appdata\local\programs\python\python37-32\python.exe" "C:\Users\User\AppData\Local\Programs\Python\Python37-32\Scripts\pyinstaller.exe" '

stable grail
#

looks like you have a python installation that is not setup correctly for this Jack. is python in your Path?

#

!resources

fallen slateBOT
#
Resources

The Resources page on our website contains a list of hand-selected learning resources that we regularly recommend to both beginners and experts.

stable grail
#

@thick bay here you go

unkempt rock
#

looks like you have a python installation that is not setup correctly for this Jack. is python in your Path?
@stable grail yes it is

wide shuttle
#

This channel is specifically for discussing Python itself, not for help questions. For short questions, we have #python-discussion, and for longer help converations, we have help channels.

#

We want to keep this one channel to discuss Python itself, from a higher-level perspective.

wary epoch
#

hello friends, I have a dumb issue 😅 , I wanna use StringVar but I don't know where it is located, what should I import to get it ??

stable grail
pliant rover
#

@wary epoch see if this helps

fickle minnow
#

thats what i mean

#

by C arrays and stuff

stable grail
#

so how would i use this @fickle minnow ?

fickle minnow
#

@stable grail same as in C, for language development and making virtual machines

stable grail
#

im not sure i see my need for this, but this chart looks very much like python to me. but if its useful and not already implemented into python, sure, go for it 😄

pseudo cradle
#

Looks like Python probably because smart pointers are already taken for granted in Python

spice pecan
#

The contents of the image look like a standard dynamic array, akin to python's list, Java's ArrayList, C#'s List, etc

#

In fact, that's exactly how C#'s List works, doubling the underlying array's capacity when out of space

pseudo cradle
#

I was about to say, sounds like any dynamic array

#

yay computer science

#

Well, not any ofc

#

You can custom make a dynamic array if you know the general behavior

spice pecan
#

Usually the difference is how the array reacts to running out of space

#

C# doubles the capacity, CPython increments by 1 iirc

pseudo cradle
#

Oh, really?

#

That's interesting

#

I'm always used to the default being double cap

spice pecan
#

I expected that to be the case as well, but when I dug into source code I was surprised to say the least

unkempt rock
#

Really? Does it just reallocate to one more?

pseudo cradle
#

That's so interesting

peak spoke
unkempt rock
#

Because that sounds awfully slow :P

pseudo cradle
#

lol what is this

unkempt rock
#

userbot

pseudo cradle
#

@unkempt rock Agree

raven ridge
#

That's not correct.

pseudo cradle
#

There's no way you're getting an amortized cost anywhere near O(1) with single increments

#

Or even fixed increments

raven ridge
#

incrementing by 1 wouldn't give amortized constant time, and Python's list does.

#

yep, you need a fixed percentage.

spice pecan
#

My bad, missed a part of it

#
 /* This over-allocates proportional to the list size, making room
     * for additional growth.  The over-allocation is mild, but is
     * enough to give linear-time amortized behavior over a long
     * sequence of appends() in the presence of a poorly-performing
     * system realloc().
     * Add padding to make the allocated size multiple of 4.
     * The growth pattern is:  0, 4, 8, 16, 24, 32, 40, 52, 64, 76, ...
     * Note: new_allocated won't overflow because the largest possible value
     *       is PY_SSIZE_T_MAX * (9 / 8) + 6 which always fits in a size_t.
     */
    new_allocated = ((size_t)newsize + (newsize >> 3) + 6) & ~(size_t)3;
    /* Do not overallocate if the new size is closer to overallocated size
     * than to the old size.
     */
    if (newsize - Py_SIZE(self) > (Py_ssize_t)(new_allocated - newsize))
        new_allocated = ((size_t)newsize + 3) & ~(size_t)3;

    if (newsize == 0)
        new_allocated = 0;
    num_allocated_bytes = new_allocated * sizeof(PyObject *);```
#

They dealt with it in resize rather than the function responsible for appending

pseudo cradle
#

I was really impressed with timsort

unkempt rock
#

Ah, good to know

pseudo cradle
#

Amortized memory cost of O(1) and worst case time complexity of O(nlog(n))

#

But still capable of O(n) when list is presorted

#

It's kind of genius

#

I can think of usecases where it's suboptimal, but I have to really think about it

raven ridge
#

It's the default sorting algorithm in quite a few languages these days.

pseudo cradle
#

I think the only "downside" is that it's not trivial for the average person to create from scratch

spice pecan
#
static int
app1(PyListObject *self, PyObject *v)
{
    Py_ssize_t n = PyList_GET_SIZE(self);

    assert (v != NULL);
    assert((size_t)n + 1 < PY_SSIZE_T_MAX);
    if (list_resize(self, n+1) < 0)
        return -1;

    Py_INCREF(v);
    PyList_SET_ITEM(self, n, v);
    return 0;
}

The call to list_resize(self, n+1) was a little misleading, but I guess it does make sense to "hide" the overallocation

pseudo cradle
#

Yeah, looks like a pretty solid replacement over quicksort

spice pecan
#

timsort is just plain amazing

pseudo cradle
#

I would need to know a lot about the data set or implementation to choose anything else

#

Or if I had literally 0 additional memory to play with

#

But I can't really think of real world issues where that matters. Only for like... leetcode questions/academia

#

How bad of an idea would it be to fold numpy into stdlib?

spice pecan
#

I don't think it would be all that unreasonable, it's used extremely widely and I believe some of the features were added specifically for it

#

ellipsis and __matmul__, iirc

pseudo cradle
#

Oh, what? matmul is std lib?

raven ridge
#

it's part of the language, though not really used by the stdlib

pseudo cradle
#

Oh

spice pecan
#

No built-ins implement matmul I think

pseudo cradle
#

I guess it lets you create your own objects that can be matmul'd I guess

raven ridge
#

right.

#

It's proxied by a few things - MagicMock, etc - but no builtin types implement it in a meaningful way, AFAIK

pseudo cradle
#

Tell more more about ellipsis

#

I've only heard it mentioned once or twice

spice pecan
#

... literal

pseudo cradle
#

oh

#

I thought it was a new lib or something

raven ridge
#
~>python2 -c "print(type(...))"
  File "<string>", line 1
    print(type(...))
               ^
SyntaxError: invalid syntax
~>python3 -c "print(type(...))"
<class 'ellipsis'>
spice pecan
#

It's used as an "autofill" kind of thing for numpy slices and it also has some use in the typing module I think

pseudo cradle
#

Huh, okay

#

Interesting

somber halo
#

never used that literal I believe.

pseudo cradle
#

Yeah, same

raven ridge
#

type stubs use it regularly, and it's used by the @typing.overload decorator

somber halo
#

well, you're into Python for some months now

raven ridge
#

it doesn't have to be - all the typing use cases can be replaced with pass, AFAIK - it just looks cleaner.

somber halo
#

I've been using Python for years and years now.

pseudo cradle
#

Haha, gotcha

spice pecan
#

Some people also use it in roughly the same contexts as pass

def func():
    ...

Although I think these should really just use pass

pseudo cradle
#

Yeah, I'd just use pass in those cases

#

It's more clear

raven ridge
#

how long 3-only? Cause you couldn't have used it when writing 2 or 2/3 code.

spice pecan
#

I haven't seen many examples of this, it's one of those things that gets mentioned as a "Python fact you might not have known!" more often than it is used

peak spoke
#

pass is a statement so you can't replace ... with it in thinkgs like Tuple[str, ...]

spice pecan
#

IMO using ... in place of pass only makes sense in .pyi stubs, since it draws... less attention I guess

peak spoke
#

I like it in abcs or as a placeholder for code

pseudo cradle
#

What would Tuple[str, ...] do?

peak spoke
#

From typing, it specifies a varialble length tuple of the specified type

pseudo cradle
#

placeholder for code sounds legit

#

gotcha

#

So it's a way to code when you don't know what's going to go in there but you don't want to cause syntax errors?

spice pecan
#

Essentially, yeah

#

The same as pass really

pseudo cradle
#

Got it. Handy

spice pecan
#

But since those are two different literals you can give them distinct meanings in your projects

#

As in "pass means nothing should be done, ... means the code just isn't quite there yet"

pseudo cradle
#

Right

#

Very cool

spice pecan
#

Though I think something more explicit, like NotImplemented, should be a better option, at least IMO

shy vine
#

Or, in Guido’s words:

Some folks thought it would be cute to be able to write incomplete code like this:

class C:
  def meth(self): ...
  ...

and have it be syntactically correct.

raven ridge
#

I mean, it is cute. 😄

pseudo cradle
#

haha

raven ridge
#

and in the case of typing stubs, ... makes more sense than pass. If you used pass it would read as "and the implementation does nothing", whereas ... makes it clear that it's "and the implementation isn't shown here".

pseudo cradle
#

Right

#

I'm a little confused on how numpy inspired this, though

#

or how it relates to folding numpy into stdlib

remote trench
#

with selenium is driver.switch_to.frame(0) related to z-index or frames themselves?

peak spoke
#

I don't know if numpy influenced that, but it's used to signify recursive containers

raven ridge
#

I think that's been there since the very early days of the language - I'm sure that predates numpy

#

containers have always been able to be recursive, and there's always been a need for every object to have a repr

pseudo cradle
#

n[1,...,1] # equivalent to n[1,:,:,1]

#

Oh what

raven ridge
#

not in general, but in numpy.

spice pecan
#

In case of recursive containers it's just three dots, not really ellipsis

pseudo cradle
#

Okay, cool

raven ridge
#

yeah. it's an ellipsis in the punctuation sense, not ellipsis in the Python object sense.

shy vine
#

I think numpy leveraged it more than inspired it, the Ellipsis object has been around for a long time

#

But it’s hard to do history on a phone

raven ridge
#

only since 3 - so sometime in the last 10 years...

shy vine
#

It was a literal in 3

#

The object is older

wide shuttle
raven ridge
#

ah, TIL.

pseudo cradle
#

Dumb question, what's a literal?

raven ridge
#

a way of constructing an object without calling that object's constructor.

pseudo cradle
#

Got it

raven ridge
#

int("5") is equivalent to 5. 5 is an integer literal.

pseudo cradle
#

Okay, that makes sense

raven ridge
#

likewise for {x:y} for dict literal, "x" for str literal, etc.

pseudo cradle
#

TIL

stable grail
#

[] and list()

pseudo cradle
#

Yeah

stable grail
#

and so on

pseudo cradle
#

Never knew what that was called

raven ridge
#

0xFF and 0b0100 for other types of integer literals.

pseudo cradle
#

Can you make a literal for a class that you make?

#

Can't think of a usecase, just curious

raven ridge
#

C++ supports that, but Python doesn't, AFAIK.

pseudo cradle
#

Okay

raven ridge
pseudo cradle
#

Oh, nice

spice pecan
#

You can get creative in some cases

#

Someone I know used slice literals for a Time class

#

Time[20:35], because why not

pseudo cradle
#

haha

#

nice

zenith topaz
#

Pathlib uses division for concatenation, always like that one.

spice pecan
#

Yeah, I've always found these kinds of things pretty neat

fickle minnow
#

Damn

spice pecan
#

Reminds me of how << and >> are used for streams in CPP

raven ridge
#

I've always found / in pathlib to be just as gross as << in C++ streams, heh. Too clever by half.

#

Using the left shift operator to mean "stream insertion", or the division operator to mean "path concatenation", is exactly the sort of thing that every OOP design book tells you not to do with operator overloading.

pseudo cradle
#

I never was that big a fan of '''<<'''

#

Oh

#

<<

spice pecan
peak spoke
#

Can't speak about << but I think it's pretty natural for paths (except the whitespace maybe) for example base_dir / "file_name.ext"

spice pecan
#

I don't really mind it personally

peak spoke
#

I'd probably use joinpath for something longer but for 2-3 it looks fine and behaves as expected imo

spice pecan
#

If an operator is rarely used and/or has little to no literal application for the class, it's tolerable

zenith topaz
#

Like matmul I guess, that one is very limited in use.

spice pecan
#

matmul, bitwise operators in most contexts, etc

zenith topaz
#

The way bitwise operators are used for sets and dicts are okay with me.

raven ridge
#

/ is hardly a rarely used operator, but it's true that it doesn't have a natural application to paths.

#

I'd argue that's not a good enough justification to give it an unnatural application, heh

spice pecan
#

I think it's fine as long as it's not the only way of doing it

zenith topaz
#

Using / for a path seperator is a very natural application imo

inland acorn
#

the natrual application for paths is to join them, but that is my opinion

raven ridge
#

There should be one-- and preferably only one --obvious way to do it.

#

the natural application of the division operator for any object is to divide a numerator by a denominator.

inland acorn
#

list_a += list_b vs list_a.extend(list_b)

remote trench
#

"Poll the DOM for a given time tying to locate an element" does polling mean to keep checking if a resource is available?

zenith topaz
peak spoke
#

It's a nice thought but there aren't all that many cases it applies to except on the overall logic of something

zenith topaz
#

I honestly don't care if you for example use the @ operator for something else than matrix multiplication.

pseudo cradle
#

You can use @ for matrix multiplication? TIL

peak spoke
#

It wraps the same method as joinpath does for convenience and to some nicer looking syntax, but using it to chaing a lot of paths together won't be as nice and will have worse performance

inland acorn
#

use them in a way that makes sense is my opinion, and using / to join paths makes sense

peak spoke
#

Not in python directly (as in on the builtin)

zenith topaz
#

Yes, @ is the matrix multiplication operator in python.

raven ridge
#

the magic method for the @ operator is __matmul__

paper echo
#

i kinda wish they didnt call it matmul

#

just call it "unassigned custom binary operator" or something, __binop__ idk

#

and believe me i am a major beneficiary of @ existing

#

idk i guess you can use it for anything if you want to

pseudo cradle
#

I have a salt rock lamp in my room 🙂

paper echo
#

nice

unkempt rock
#

anyone know if python have anything to a struct in c or is tuples the only way to group datatypes?

spark magnet
#

@unkempt rock you could use a dict, or define a class

unkempt rock
#

seems kinda excessive with a class with just variables

spark magnet
#

attrs or dataclasses are good for simple value classes

unkempt rock
#

ima look into dataclasses

#

dataclasses seem pretty promising on doing what im trying to do

#

tx

boreal umbra
#

@unkempt rock in my opinion named tuples are the most similar to C structs. They're tuples for which each index is associated with an attribute name.

unkempt rock
#

apparently dataclasses r very similar too

boreal umbra
#

Data classes are more substantial than C structs. They can have methods.