#internals-and-peps
1 messages · Page 54 of 1
eh I just need to keep track of insertion
basically working on new enums implementation
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
because current one is written in a sophisticated way and it also uses sunders (ew)
(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
@peak spoke maybe this could help https://docs.python.org/3.2/library/sys.html?highlight=sys.intern#sys.intern
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.
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?
sure
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
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.
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
hello can anyone link me any free course websites for python for beginners please?
!resources has some links
The Resources page on our website contains a list of hand-selected learning resources that we regularly recommend to both beginners and experts.
k ty
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 usedocs\.python\.org\/(\d)\.\d+\/=>docs\.python\.org\/\1/
which extension do you use for that?
also what if i WANT to visit the old docs, or beta docs, at some point
I have bigger problems with completely outdated docs like this https://docs.python.org/2.4/lib/typesseq.html
an even bigger question: why does this actually happen? misconfiguration on the python site?
@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.
How different the python virtual machine differ from the lua virtual machine?
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
@clear coral how would a register based vm work? Don’t u still need stack frames?
@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.
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
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
@steep mango this is not a help channel. please either ask in #python-discussion or see #❓|how-to-get-help
the question seems to fit this channel imo
falls under the 'overall design of the language' purview
that was my though, why python does that xD
hm alright, i guess so
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
oh I didn't know that, got any source? nvm, found it, reading https://www.python.org/dev/peps/pep-0617/
!pep 617
!pep 617
lol
jinx :p
we 3 rock xD
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
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.
so you have to use the flag to enable the old parser?
Yep, last I heard, and unless the PEP is outdated
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
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
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
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
What do you think about introducing constants on python (not typing.Final)?
What would be the merit?
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
Immutability by convention works out just fine in most cases imo
if someone is intentionally breaking a contract of your variable, they probably have a reason
SCREAMING_SNAKE_CASE already tells programmers "do not modify"
Some major IDEs will warn you if you reassign those ^
SCREAMING_SNAKE_CASEalready tells programmers "do not modify"
@flat gazelles/tells/yellshahahaha

😄
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.
consts also would be good for code protection
imagine overwriting accidentally a variable that mustn't be modified 
as @brave badger and @flat gazelle said the convention just works fine.
well okay
and python generally does not let accidentally modify things. You need explicit global to be allowed to modify those
i just wanted to hear your opinion 
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
yeah
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.
MyPy to the rescue!
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...
simple prototypes in c 
just because you havent got to worry about any of that just to get a basic idea
yeah
thats a big advantage of python
compared to another languages
for example c
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__)
@bitter belfry This channel is not for help, and we will not spoon feed you answers
Oke, my fault
What is factory pattern in python?
It's using classes to construct objects yes.
@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.
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
PyPy is faster if your tasks are longer
however, for all pure python tasks, its lovely
and providing its compatible which it can be relevant as things like dockerized pypy are very awkward
PyPy is also slow for short tasks
Guido, the creator of Python said if you need speed, use PyPy
so theres your endorsement
eh sure, if you can get away with using PyPy use it by all means but it is very hit or miss with compatibility
yeah, not perfect. its why i still use cPython mainly, i just use too many different libraries that PyPy doesn't support
mhmm
indeed
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
depends on the task IMO
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
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
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
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)
Does tracemalloc shows all the memory allocated by the Python's process?
doesnt seem to give me any useful information
ik this is some sort of win32 syntaxical stuff but idk exactly whats up
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()
i think ur escape chars r still gonna come out
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
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.
with open(filename, 'r') as reader:
for line in reader:
print(line.strip("\n"))
You could just do this
: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).
!ban 730183111375061054 joined to spam chainmail
:incoming_envelope: :ok_hand: applied ban to @burnt stag permanently.
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++
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
okay, that was what I was thinking. Thank you f1re :D
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?
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.
I forgot. When you make a module is the init file run first or last?
First.
initialize
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.
I posted the code in #help-croissant
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 havingsubmodulewith__init__.pyandfile.pyand then doingfrom .submodule.file import SomeClass?
@shy belfry Yes
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.
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.
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.
Why does this channel keep getting marked as unread?
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.
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.
If you have googled for other places and haven't found any, odds are none of us will know any alternatives either
(besides, this is the wrong channel for such a question, #❓|how-to-get-help)
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.
Ah, I see. Well, good luck!
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?
iirc one optimization that it does is constants
I don't think there's much beyond peeophole optimizationd
You get immutable literals turned into constants, simper dead code it excluded etc.
Seems like python would be hard to optimize because lots of expressions could secretly be something else.
yea it makes sense given everything is performed in the runtime
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?
was wondering the same thing hehe, probably yes
You could partly call cython that
that's different because it straight up transpiles to C and then compiles it (iirc)
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?
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
is there someone here who can help me out?
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 thePython/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.
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.
Very iluminating, thanks 🙂
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.
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
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.
to be honest in a language like javascript, TS is a godsend (imo)
Absolutely agreed. It forces you to be more wary.
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
There was seperate attempts / projects regarding LLVM+Python, but I don't remember if any of those made type checking mandatory.
hey, isidentical :D
In my opinion, python is way too dynamic to fully implement mandatory typing to be honest.
numba is like Cython + PyPy approaching
Cython transpiles then compiles C code, PyPy does have a JIT compiler though just like numba
Then again haven't used numba much
any more notablee llvm py projects?
@brazen jacinth I have a few here speakerdeck.com/isidentical/llvm-and-python
that slides also includes a simple (and very very tiny) llvm compiler for annotated python source code
awesome, i'll take a look
We talked about bools in #python-discussion and I remembered one of my pet peeves with python
why is '' not falsy?
it is
??
indeed
Am I braindead?
In [13]: bool('')
Out[13]: False
Oh my god all this time
it is a general concept, emptiness is just falsy
it would be everyone's pet peeve if an empty string was true ;P
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
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
Indeed it is, check out also this http://qinsb.blogspot.com/2011/03/unladen-swallow-retrospective.html
I wrote this while I was at PyCon, but I kept revising it. Anyway, here goes. :) As is apparent by now, no one has really been doing any ...
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 🙂
can someone explain to me what a init is im new to python
@undone birch @unkempt rock take a look at #❓|how-to-get-help
@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
@unkempt rock Thanks!!
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.
ya know i never really appreciated python's simplicity until i moved to c++ and i was like damn thats a lot of typing
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.
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
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.
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
I was going to say
How much type coercions does Python actually do?
I generally just get typeerrors
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
hello everyone, I was wondering if this is the right channel to ask some questions about python's coding standards?
perhaps yeah, you can review the channel topic and if you feel like the question fits, you are welcome to ask here
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?
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
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
@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
I'm making assumptions about what they're using it for/implementation, I guess
What are your assumptions
I'm assuming that they're performing multiple calculations with the same number and will thus run into compounding float errors
Interesting, so I guess using sympy could avoid floating point errors there
Yeah, or at least not have them compound
Hadn't thought of that usecase before
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
that's off-topic here
you could try asking in #python-discussion or an off-topic channel
or even #web-development
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
All floats are equally large. If you need so much precision that float math is an issue,do not use floats, use Fraction
Or possibly Decimal
Hey everyone. I'm new here 🙂
hey
Hey Pythonite
This channel isn't for introductions.
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 🤔
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:
- almost all of them are them assigned as instance attributes so I have bunch of
self.param1 = param1 - 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
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
Source code: Lib/dataclasses.py
This module provides a decorator and functions for automatically adding generated special methods such as __init__() and __repr__() to user-defined classes. It was originally described in PEP 557.
The member variables to use in these generated methods are defined using PEP 526 type annotations. For example this code:... read more
@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 🤷♂️
well, you could set default values to the args
- 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?
defaluts won't really help as most of those params come from DB
There can be legitimate reasons to have many args and in that case, splitting them up into dataclasses just makes things more cumbersome.
@oblique crystal maybe write a classmethod, like Obj.create_from_db(db)
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.
maybe get them as a dict and do class(**{'name': 'peter', ... })
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
ye, at that point you are making a very rudimentary ORM essentially
Yeah but then you’d have to do **obj.dict and you wouldn’t even have control over special behaviour
well this works for function
you need to be a mapping
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()})```
@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'}
the python interpreter eval switch case is so big, it breaks some C compilers 
You can read up on it here https://docs.python.org/3/reference/datamodel.html#emulating-container-types
If you want to support
**you need akeysand a__getitem__method
@Wattle
How come.keys()isn't a special method, if it's used internally by Python ? 🤔
You'd have to ask a Python core dev.
@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…
probably backwards compatibility
seems unnecesary given it doesn't add any benefit
Well, it does, imo. Otherwise special methods wouldn't have a different pattern in naming + a conventionnal signature
You only need keys if you want to support ** for your classes which I can't see being a common case
for example file-likes also use non dunders afaik
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
any promising active projects working on removing the GIL?
im guessing not since it's practically engraved in the implementation 🤷♂️
If I recall correctly that was the latest effort in that regard
😋
You have IronPython and Jython, I think neither of those use a GIL
removing the GIL is not difficult, neither is replacing the locks. The issue is the fact Python becomes unworkably slow as a result
true, but only as 2.7
I'm pretty sure it's about as much of an issue for py3
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?
it was more like 16 times from the talk I heard on the subject
oof, ok fair enough
and really, the GIL is only a problem for problems that
- are CPU bound and benefit from being performed 4-32 times faster
- would have large IPC overhead when multiprocessing
which is extremely narrow
wouldn't exactly call the cpu-bound problem set narrow
but you're right in that regard, it doesn't really matter in py
meh, large overhead and ipc is a pain
which has almost no overhead
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
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.
yea
or use something like numba
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
I've gotten used to using PyO3 if I ever run into situations where I need IPC
sometimes Kafka as well
or Redis
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
Processing libraries can also utilize thread parellelism in the non python code, which usually is present for anything that takes more time
@brazen jacinth I specifically mean Redis' Pub/Sub feature
keeping locks on everything to accomodate the removal of the gil would hurt most of python's uses
pub/sub?
It's fairly lightweight and gets the job done well for simple IPC
publish/subscribe channels
ahaa
basically interprocess messaging
I wonder if asyncio.ProcessPoolExecutor supports shared memory
never used redis for anything more then a polished memcached
channels seem to prefer it as a way to communicate across websocket connections from possibly multiple server workers
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
!pep 554
interp = interpreters.create()
print('before')
interp.run('print("during")')
print('after')```
holy guac, free isolated code?
this is an amazing pep
Is this as inteneded?
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
Oh but if i do the same thing like
a=1
b=a
b=2
print(b) #returns 2
print(a) #returns 1
Hi
Im a new programmer
I just need help to fix the problem with my Fibonacci generator

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
please visit #❓|how-to-get-help @fathom harness
ok
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
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 not sure if it was mentioned but a lot of classes implement a copy method. list has one, for instance
@tawny shoal oh thx
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)
Immutables vs Mutables, basically: https://medium.com/datadriveninvestor/mutable-and-immutable-python-2093deeac8d9
What are your thoughts about Kite? It looks really promising and useful to me https://medium.com/kitepython/kite-announces-intelligent-snippets-for-python-10e3205318c
how to convert from [[10,20,30,40,50]] to [10,20,30,40,50]
@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
kite is nice at first glance, but it's a real resource hog
afaik, maybe that's changed
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
I don't feel like it adds anything over what PyCharm provides when it comes to completion
Do you still use it, g?
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
That's often useful too yeah
Best graphics library for python?
Like gui not pure graphics
Depends on personal preference and what you intend to do with it, this is not the right channel for that though, try #python-discussion
I did try in there but it was way off topic then the admin muted the channel
That doesn't mean randomly post in other channels
[issubclass(bool, int)]
"f strings are almost always a bad idea' 🤔
wut
i just bumped into this whilst getting annoyed by W1203
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
idk but i'm getting E1205: Too many arguments for logging format string instead now
so it's pissing me off too 😄
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'
@fair mesa I'm not aware of a limit imposed by the language
But if you are going that many levels deep, you might want to take a step back and look at your design.
it's about 4 layers deep
They should be limited to around when the 80 char limit becomes not viable (even with a higher line limit used in the project)
it's basically getting data inside of a list, inside of another list, and inside of another list.
Recursion might make this easier to read.
oh lol ive done sth similar with big tables and ur gonna experience some slowness
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.
Ur scraping or calling an api?
scraping
And each country has its own page?
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
got SyntaxError: too many statically nested blocks after 20 indent blocks
huh, so 20 is my limit
If youve got 20 nested loops you are doing something wrong
least I don't come close to that
noo, I would have 1 loop and 3 nested loops inside it max.
.thats fine
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
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.
what's the best place to learn python, assuming no programming knowledge at all?
Wrong channel. Please ask in #python-discussion
oh my mistake, sorry
hi, what channel do i get help in? sorry im new
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
you could compile python3 yourself and change the limit 😉
perfect recipe to end up with code that only works for you lol
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
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.
I had a program that extended the recursion depth for a specific algorithm. I'm going to double check what I did
sys.setrecursionlimit(n)?
Pretty sure that's wht it was, lol
why tho, that just seems like a bad idea 😂
I'm just curious as to what I set it to and why
sys.setrecursionlimit(10**7) # max depth of recursion
Well, you can't set it to any amount you want.
Looks like I was doing some tree traversal and an iterative solution would have been more complex
I mean, you can, but it will just give you a segfault.
ah
https://github.com/lihaoyi/macropy#tail-call-optimization unrelated but found it while i was looking at recursion, and seemed cool 🙂
!e
import sys
sys.setrecursionlimit(10**7)
def f(): f()
f()
@grave jolt :warning: Your eval job has completed with return code 139 (SIGSEGV).
[No output]
1000
Ah, gotcha
Well, you can emulate a recursive solution by maintaining your own call stack.
But that probably belongs to #esoteric-python already :)
haha fair
Here's what I don't get
Why does a python compiler let you redefine a function in the same file?
I don't really get what collections.ChainMap is for
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
won't we also be getting | for dicts anyway?
I don't know
They would not remove this if the pipe got added
Breaking change. And it's not quite the same thing
would it be preferred to use ChainMap over d |= (d1 | d2)
hey quick question, is there another way of doing large commenting out of stuff other than the # for each line?
a docstring
ah nevermind I figured itout
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.
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 😄
no problem!
Hey there , I am new to python 🐍
hi
I use vscode editor with jupyter extension
cool
Just started a week ago ,,I am taking python course from Udemy from Jose
hey chinz, this type of discussion would be better suited in #python-discussion
is it a free one?
im assuming so
Ok
This channel is more for advanced topics about the Python language itself, as opposed to getting help or learning the basics
@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
c, templates... 🤔
Actually I shouldn’t say that, someone’s probably implemented templates with macros
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
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.
In this pic I have two functions and I want to call them inside another third function .
How to call a function inside another function in python ?
The same way you would call any other function, shuffle_list().
#python-discussion is more suited for these questions though.
Ok @shy belfry now I will post these questions in #python-discussion
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 afoo = ...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)
Looks like something I'll need to read up on. I've never really come across it in C++.
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.
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```
Ok people, docstrings. reST or Google? I wanna hear your preferences.
Ok people, docstrings.
reSTor
@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 afoo = ...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 fordef 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
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
C isn't even object orientated though
can't really compare
Personally I prefer a structural form of programming, but thats just me
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.
^
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)
That is a great point, I always thought that made so much more sense intuitively, how that syntax was used in R.
I think def statements increase readability, but they do hide this a bit
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
There's (at least) one thing that's different
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.
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
That's why we call functions created through lambdas "anonymous functions", because they don't have a name
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.
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
i think you should visit #❓|how-to-get-help @dense wraith
please read the topic of this channel
Well, the fact that functions are an object came from how cpython is implemented : (almost) everything can be casted to a PyObject, and then is an object, which mean that it can be referenced, has a type and a reference counter https://docs.python.org/3/c-api/structures.html#c.PyObject
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
Oh yeah I liked the clamp suggestion.
When you're defining a function using the
defstatement, 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.
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 ...
LOL
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)
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
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)
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
i use the max(min(... construction all the time, i think it's perfectly readable, if only because i read it a lot
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.
You can take a look at collections which has some, a deque for example can come in handy
Im familiar with most of those.
@unkempt rock Queues and stacks are required when ur using threads
and hashing for data storage
finger trees are amazing
though in python you can mostly use lists/deque for the same purpose
Graphs and the different ways of representing them don't have quite so many practical uses, but are worth learning about @unkempt rock
Has anyone ever used descriptors to restrict the value of an input argument to a class init or function ?
Kinda the way this person does it https://foss.heptapod.net/openpyxl/openpyxl/-/blob/branch/default/openpyxl/descriptors/base.py
Hello everyone.
Are you also feel frustrated with Python's idiotic errors such as
"can't multiply sequence by non-int of type 'float'"
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
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.
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
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
One you deserialize JSON-formatted text, you are working with Python lists and dictionaries
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.
is that actually true.
python is python, but numpy is mostly a C module
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 Wow. It is weird. I assumed/guessed we have numpy arrays to calculate fast.
No, that's not true
Numpy arrays are laid out as arrays in memory
They're not slight modifications of Python lists
laid out as arrays in memory, as opposed to contiguous python lists which aren't? 🤔
Yeah, I haven't expressed myself that clearly
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
I figured out: numpy.asarray(mylist) solves problem.
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
you could imitate some of the behaviour with the aforementioned array.array
I'm simultaneously trying to find the relevant parts of the numpy source code
oh no :/
But numpy also keeps things like iteration in C exposed to python through methods, allowing it to be a lot more performant
see you next week fella
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
How hard is it to write C level stuff and make python bindings for it?
Depends what you want to do
Generally not that hard
More tedious
#c-extensions is there if u wanna try it 🙂
@swift imp it's easy but u need to know how both languages communicate like wrappers
I need more practice with it, seems like an incredibly useful tool
I find it quite amazing that we can use a compiled language like that in python
In what cases should we use abc.ABC based classes?
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
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)
django is a package, http is a module, HttpResponse is a class in the http module
well I didn't wanna take up 20 mins for a question that can be answered easily
Again, this is not the appropriate channel to get help from.
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.
ah alright got it
Hey guys
I’m a python developer specializing in Django, machine learning, deep learning and cyber security
sup, u do any ctfs?
@sullen widget what about changing your profile pic ?. i think you should respect others religion!
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.
Let's please try to keep this channel on-topic
if you'd like to speak to our moderators, feel free to send a DM to @summer lichen
agreed
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#?
python and then if u need like super optimized code learn C++
Hello can anyone introduce me with namespaces in python ?
C++ is used when python is compiled and if u likkme need more control over that process u can
i am new here
be specific
@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
I am giving a interesting python project challenge. Would all of you like to solve it?
Type "yes" if you want to
yes
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
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
@lime dagger I would suggest taking this to another channel
Who else here has a love-hate relationship with descriptor protocol?
I mostly have a love-love relationship with it
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.
hey I needed some help with creating a .py file into an exe could anyone help?
#python-discussion though the short answer is to use Py2Exe or PyInstaller.
when I tried to do it
Any good resources to learn Python? I’m new to programming
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" '
looks like you have a python installation that is not setup correctly for this Jack. is python in your Path?
!resources
The Resources page on our website contains a list of hand-selected learning resources that we regularly recommend to both beginners and experts.
@thick bay here you go
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
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.
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 ??
this is not a help channel @wary epoch please use #python-discussion or pick a help channel to ask your question. look in #❓|how-to-get-help for more info about how to claim your own help-channel
so how would i use this @fickle minnow ?
@stable grail same as in C, for language development and making virtual machines
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 😄
Looks like Python probably because smart pointers are already taken for granted in Python
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
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
Usually the difference is how the array reacts to running out of space
C# doubles the capacity, CPython increments by 1 iirc
I expected that to be the case as well, but when I dug into source code I was surprised to say the least
Really? Does it just reallocate to one more?
That's so interesting
Incrementing by 1 would be too expensive overall, https://github.com/python/cpython/blob/master/Objects/listobject.c#L61-L70
Because that sounds awfully slow :P
lol what is this
userbot
@unkempt rock Agree
That's not correct.
There's no way you're getting an amortized cost anywhere near O(1) with single increments
Or even fixed increments
incrementing by 1 wouldn't give amortized constant time, and Python's list does.
yep, you need a fixed percentage.
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
I was really impressed with timsort
Ah, good to know
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
It's the default sorting algorithm in quite a few languages these days.
I think the only "downside" is that it's not trivial for the average person to create from scratch
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
Yeah, looks like a pretty solid replacement over quicksort
timsort is just plain amazing
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?
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
Oh, what? matmul is std lib?
it's part of the language, though not really used by the stdlib
Oh
No built-ins implement matmul I think
I guess it lets you create your own objects that can be matmul'd I guess
right.
It's proxied by a few things - MagicMock, etc - but no builtin types implement it in a meaningful way, AFAIK
... literal
~>python2 -c "print(type(...))"
File "<string>", line 1
print(type(...))
^
SyntaxError: invalid syntax
~>python3 -c "print(type(...))"
<class 'ellipsis'>
It's used as an "autofill" kind of thing for numpy slices and it also has some use in the typing module I think
never used that literal I believe.
Yeah, same
type stubs use it regularly, and it's used by the @typing.overload decorator
well, you're into Python for some months now
it doesn't have to be - all the typing use cases can be replaced with pass, AFAIK - it just looks cleaner.
I've been using Python for years and years now.
Haha, gotcha
Some people also use it in roughly the same contexts as pass
def func():
...
Although I think these should really just use pass
how long 3-only? Cause you couldn't have used it when writing 2 or 2/3 code.
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
pass is a statement so you can't replace ... with it in thinkgs like Tuple[str, ...]
IMO using ... in place of pass only makes sense in .pyi stubs, since it draws... less attention I guess
I like it in abcs or as a placeholder for code
What would Tuple[str, ...] do?
From typing, it specifies a varialble length tuple of the specified type
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?
Got it. Handy
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"
Though I think something more explicit, like NotImplemented, should be a better option, at least IMO
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.
I mean, it is cute. 😄
haha
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".
Right
I'm a little confused on how numpy inspired this, though
or how it relates to folding numpy into stdlib
with selenium is driver.switch_to.frame(0) related to z-index or frames themselves?
I don't know if numpy influenced that, but it's used to signify recursive containers
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
not in general, but in numpy.
In case of recursive containers it's just three dots, not really ellipsis
Okay, cool
yeah. it's an ellipsis in the punctuation sense, not ellipsis in the Python object sense.
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
only since 3 - so sometime in the last 10 years...
Yeah, it was there in 2.0 as well (https://docs.python.org/2.0/lib/bltin-ellipsis-object.html)
ah, TIL.
Dumb question, what's a literal?
a way of constructing an object without calling that object's constructor.
Got it
int("5") is equivalent to 5. 5 is an integer literal.
Okay, that makes sense
likewise for {x:y} for dict literal, "x" for str literal, etc.
TIL
[] and list()
Yeah
and so on
Never knew what that was called
0xFF and 0b0100 for other types of integer literals.
Can you make a literal for a class that you make?
Can't think of a usecase, just curious
C++ supports that, but Python doesn't, AFAIK.
Okay
it has been suggested as a possible future feature - https://mail.python.org/pipermail/python-ideas/2015-June/033871.html
Oh, nice
You can get creative in some cases
Someone I know used slice literals for a Time class
Time[20:35], because why not
Pathlib uses division for concatenation, always like that one.
Yeah, I've always found these kinds of things pretty neat
Damn
Reminds me of how << and >> are used for streams in CPP
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.
Can't speak about << but I think it's pretty natural for paths (except the whitespace maybe) for example base_dir / "file_name.ext"
I don't really mind it personally
I'd probably use joinpath for something longer but for 2-3 it looks fine and behaves as expected imo
If an operator is rarely used and/or has little to no literal application for the class, it's tolerable
Like matmul I guess, that one is very limited in use.
matmul, bitwise operators in most contexts, etc
The way bitwise operators are used for sets and dicts are okay with me.
/ 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
I think it's fine as long as it's not the only way of doing it
Using / for a path seperator is a very natural application imo
the natrual application for paths is to join them, but that is my opinion
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.
list_a += list_b vs list_a.extend(list_b)
"Poll the DOM for a given time tying to locate an element" does polling mean to keep checking if a resource is available?
Check #❓|how-to-get-help for info on how to get your own help channel @remote trench
It's a nice thought but there aren't all that many cases it applies to except on the overall logic of something
I honestly don't care if you for example use the @ operator for something else than matrix multiplication.
You can use @ for matrix multiplication? TIL
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
use them in a way that makes sense is my opinion, and using / to join paths makes sense
Not in python directly (as in on the builtin)
Yes, @ is the matrix multiplication operator in python.
the magic method for the @ operator is __matmul__
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
I have a salt rock lamp in my room 🙂
nice
anyone know if python have anything to a struct in c or is tuples the only way to group datatypes?
@unkempt rock you could use a dict, or define a class
seems kinda excessive with a class with just variables
attrs or dataclasses are good for simple value classes
ima look into dataclasses
dataclasses seem pretty promising on doing what im trying to do
tx
@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.
apparently dataclasses r very similar too
Data classes are more substantial than C structs. They can have methods.