#internals-and-peps
1 messages · Page 93 of 1
I'm stuck in 3.7 and I hate it
Annotations work half assed
I don't even have Literal
I hate Literal. It's a perfect example of something that's obviously not a type jammed haphazardly into this farcical type system
Is there ever a time when it matters that something is a literal? That's just a matter of how the programmer sees the code.
Pylama wasn't a fan I when I didn't use it
It was like evaling the string and complaining it couldn't find the type
Quick question how do i get my skills up in python?
There shouldn't be, but there are some things that are idiomatically always literal. Like the mode argument to open is idiomatically "r" or "w" or "rb" or "wb", etc
And Literal is a hack to allow the Python "type" system to, in some circumstances, describe acceptable values rather than types.
why is a literal not a type
https://mypy.readthedocs.io/en/stable/literal_types.html
Literal types let you indicate that an expression is equal to some specific primitive value. For example, if we annotate a variable with type
Literal["foo"], mypy will understand that variable is not only of type str, but is also equal to specifically the string"foo".
That's literally (heh) the definition. Literal specifies values.
yeah, what’s wrong
singleton types, AFAIK, have a well-accepted place in type theory and FP languages
whether their inclusion in Python is desirable is debatable, of course, but I do not think you can say they are not types
just like, for example, dependent types
which one can think of as types parametrised by values?
wouldn't an enum be the standard approach to what Literal is trying to do?
Yeah, although sometimes it is easier to just use a literal
For example, the mode parameter of open() is typehinted with Literal. If it did use an enum, opening files would be more verbose.
yes, that would be perhaps annoying
but when I write a function that only takes specific values, its hard to resist using an enum
of course nothing will prevent the caller from trying to access a member that doesn't exist, but that's their own problem, and an exception will occur in a more logical place
I guess strings are more convenient for buitins etc; Literal will also accept enum values
having to import an enum for open would defeat the advantage of open being a built-in
could use something like open.READ for example, although that feels a bit ugly
mmm
well I usually just get a Path obj even if I don't have to 
so I don't avoid the import anyway
Once you are committed to typing you can’t escape the import hell anyway
shouldn't be that bad with 3.10
What’s new in 3.10?
I thought it would have ended with generic typehints, but since you can’t do list[str] that’s pretty much useless
the 3.9 generics and the union operator
What do you mean can't do list[str] ?
!e
def foo(bar: list[str]):
...```
@undone hare :warning: Your eval job has completed with return code 0.
[No output]

pe.moveTo(132,475,1),pe.click(132,475,1),pe.typewrite(superb bro i want this because of camera),pe.click(917,515,1)
i want to loop this program for 2 hours and after the word 'camera' in the sentence i want to add non repeating unique numbers
@granite hatch This isn't a channel for general help. See #❓|how-to-get-help and open a help channel.
I guess it's an attempt at structural typing. Literal types work fine in TypeScript, for example, where structural typing is supported well:
type AstNode =
| {tag: 'if', if: AstNode, then: AstNode, else: AstNode}
| {tag: 'number', value: number}
| {tag: 'variable', name: string}
I wouldn't say it's "obviously not a type".
ABCs (like Mapping) or Protocols aren't really "types" either, you can't make instances of them.
Or, well, you can, but you don't have to.
A notion of "type" is a bit vague in a dynamically typed language. list[int] and list[str] are treated as different types, even though at runtime it's the same type list.
And Literal is a hack to allow the Python "type" system to, in some circumstances, describe acceptable values rather than types.
A type being a set of acceptable values is fine with me.Union[int, str]isn't a real Python type either, neither isIterable[float].
isn’t that what a type is?
a set of values
In a structural type system -- yes. In a nominal type system (which is closer to Python) -- no.
Well, in Python it's kind of in between.
With duck typing and such
why do you say that definition doesn’t apply to nominal typing
In a nominal system you can't just make a type (set) like: "has a method foo that takes an int and returns a str".
You can explicitly make a type FizzBuzz: has a method 'foo' that takes an 'int' and returns a 'str', but then a function that accepts a FizzBuzz only accepts FizzBuzz, not just any value that has the right method.
i.e. structural typing specifies the required properties of a type, and nominal typing specifies the required name of a type.
So you could say that in nominal typing, a type is a set.
But it's a very restricted kind of set.
Well, any type is a set of values, yes.
the constraint is just stronger than in structural typing
so in the original context, a literal type is just a singleton set
why does it have to be an attempt at structural typing?
A type being an explicitly specified set of acceptable values like {"a", "b", "c"} is not possible in nominal typing.
Unless you're using dependent types or something like that.
So im working on a roblox finder and how can i do this
{
"status": "ok",
"robloxUsername": "vTroops", --I want my python to print this specific username and how can i do that
"robloxId": 1323247939
}
im dumb sorry
Hello we are looking for dev to new project "enzy" if you know how to use python in scale 9/10 dm Me for more informations
@proud vault This server is not for recruitment. If you want to search for collaborators for your open-source project, you can go to #python-discussion or in an appropriate topical channel.
@unkempt rock This isn't a channel for general help. See #❓|how-to-get-help and open a help channel.
ko k
@gleaming rover You can make a type which has only one value. But you can't make a type which is a single value that already belongs to some other type, at least in a nominal type system.
Again, I'm not saying Literal doesn't have a place.
In other words, structural typing has the ability for a type to "pick out" values from some other type (like str) without creating a subtype that the value must nominally belong to.
why not?
okay this is my reasoning
it’s a special case of subtyping
Maybe we just have different definitions of 'nominal' and 'structural' typing.
nominal typing just means that types are explicitly declared by name, as opposed to members, right?
I guess.
In nominal typing, A is a subtype of B only if A is explicitly declared a subtype of B
So if you have a value like "foo", you can enumerate the types it is a subtype of: str, object, Iterable[str] (suppose it inherits from it explicitly, for the sake of argument)
but in structural typing, a value can have infinitely many types: Any, Literal["foo"], Literal["foo", "bar"], {sequence of odd length}, Union[str, int], ...
so then, the question is - what is a "subtype"?
and normally we talk about substitutability, right
In a nominal type system, a subtype of A is something which inherits from A.
But in structural typing, there might be more complicated rules depending on the type system.
In Python it's "it depends on the type checker because they are not standardized and have conflicting ideas"
must this be true?
Can I somehow bypass this "Caused by SSLError(SSLError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:852"
That's the definition of a nominal type system, in my books. Otherwise, what's the difference between nominal and structural?
let me think of how to express this
okay, so the constructor of A takes a number of parameters. there exists a set of parameter tuples, from which a set of possible values of A objects may be generated.
why can't a subset thereof form a subtype of A? these are all indeed members of the type A, independent of their structure.
I don't have the type theory necessary to express this idea
but it does not seem logical to me that singleton types cannot exist in nominal typing...?
what do you mean by 'singleton type'?
types with a single member
i.e. literal types
...
actually, aren't literal types a special case of dependent types
if you think of dependent types as type constructors (at least partially) parametrised by values
In abscence of dependent types, you can't ever be sure that you can pass some value to a function that accepts this subset-of-As
Because if you have some random blob of things, you also need a proof that it forms an A.
In nominal typing, it's determined by the names of the types and their inheritance hierarchy.
The only difference in structural typing is that a type doesn't have to be a name, it can also have some structure, like Literal["str"] or {x: number, y: number}, so that subtyping looks at the structure of the type, not at its name
No
because (in a nominal system) a value can't belong to two types at the same time, except if one is a subtype of the other, in which case you can only specify one.
although...
multiple inheritance?
yeah, that's what I was typing after the although
you can have a value that implements a number of interfaces or just inherits from multiple classes
but
I actually don't know
I don't know any language with a nominal type system that has intersection types, let's say it like that 🙂
Scala?
huh?
Scala, the language
I know, I just didn't know it has intersection types
although it does support structural typing, but that comes about through reflection
actually
does Haskell have structural typing...?
I don't think so
but there are so many extensions that it probably has type-level minesweeper in one of them
I guess intersection types can exist in nominal typing then.
if we accept that
isn't it also true that the intersection type is not externally defined?
for example, you can have a value of type A & B without defining that type elsewhere
yes
and nevertheless, it will not be compatible with a structurally identical type C & D
I don't follow
say you have these classes (Python syntax)
class A:
left: int
class B:
right: int
class C:
left: int
class D:
right: int
A & B exists as a valid intersection type, and no values of type C & D are members of that type, despite being structurally identical, in a nominal typing system
my argument is that a literal type is related to this idea - that types need not exist as externally defined entities that may be independently instantiated, as long as they exist as some refinement of an existing type, or an independently defined type
well, you could have a type X inheriting from A, B, C, D, which is a subtype of all of both, but then it turns into madness
But you can't have an A[5] or (A & B)[4, 2]
indeed you could, though that would probably be the diamond problem
right?
not in this case, yes
but now
A & B is really {all possible values of A} & {all possible values of B}, right
is it that big a leap to think of, say, Literal['this'] as str & {'this'}?
you mean str & {'this'}?
but the issue is that you can't define a type {'this'}
str & {'this'} = {'this'}, so the intersection doesn't change anything
I'm thinking of it as more a refinement (? not sure if right word) of an existing type
or rather
let me put this another way
it's basically a special case of dependent typing
yeah, I guess
e.g. in a dependently typed system, even ints are a subtype of int
and you can express that with a type-level predicate
a literal type just has a predicate applied that is only valid for one value
...is what I'm thinking
yes, I suppose it's some kind of dependent typing
because it's mentioning values at the type level
ye
this is what I'm thinking
well, in a dynamically typed language it's a little bit unclear and vague
you could say that a structural type {x: int, y: int} is a type Any refined with
lambda v: (
hasattr(v, "x")
and hasattr(v, "y")
and isinstance(v.x, int)
and isinstance(v.y, int)
)
@gleaming rover ^
or that a nominal type str is a type Any refined with lambda v: isinstance(v, str) (ignoring instance check hooks)
indeed it is
especially in Python
but I was thinking more in the general sense
Generators/iterables are what fuck up the entire idea of type checking right
Like run time checking
huh
why
I've toyed with decorators that take type hints and use them to do all sorts of type and value checking
But if something returns a generator, I just consumed it
It's actually kinda ez to take type hints and use them for run time checking
But generators fuck it all up
@swift imp
It's actually kinda ez to take type hints and use them for run time checking
That doesn't work for functions either
i.e. when you pass a function to another function.
Right u can't check the input and return types
Another point is that it can take a lot of time. For example, if you have a binary_search : (Sequence[int], int) -> Optional[int], you'll have to go through the entire sequence to make sure it only has ints, which makes this function 100% useless.
and you can't really check generic types at runtime
So in general, type-checking is a static thing. Type errors ought to be caught (mostly) at look-at-your-editor-time or at automated-build time.
The only thing I miss about compiled languages
All those sweet sweet errors to make debugging ez
Well, you can use type hints, and you'll catch quite a lot of type errors that way
@swift imp Well, yes. Type hints don't cover the whole language.
I love descriptors
although in statically typed languages, descriptors don't really exist, right? 🙂
True
TypeScript uses gradual typing as well, but as long as you aren't using any, it's pretty safe in my little experience of using it.
I do like Literal and structural typing to work with an existing codebase or external input that isn't really statically typed, but without adding too much runtime overhead
like hmmm, say, passing specific keys you expect in JSON dictionaries you've received, but without using an Enum
Literal makes it easier to pass the specific key value you expect without creating a whole new type for it
but I always prefer using my own types from the start where and when I can
I assume this kind of thing may be easier in more functional languages where everything is statically typed from the start? but in the meantime structural typing is kind of helpful
from typing import ParamSpec
_P = ParamSpec("_P", bound="Client.start")
class Client:
async def start(self, token: str, *, bot: bool = ..., reconnect: bool = ...) -> None: ...
def run(self, *args: _P.args, **kwargs: _P.kwargs) -> None: ...
```can any typing wizards tell be if this use of bound is valid?
i cant tell what the other use of the bound would be from the pep or the doc for it
@raven ridge If you dislike Literal, you will hate this https://mail.python.org/archives/list/python-dev@python.org/thread/NOLCFYLYAQQHXISNMPYCEOAZ7ZPFCGUW/ even more, I suppose
🙂
I want to make a cmd like when i use it it send a particular msg to a user in DMS like
+DM (ID) (MSG)
ping me if u know how to do it
This isn't a channel for general help, this is a discussion channel. If you need help with discord.py, you should open a help channel (see #❓|how-to-get-help) or ask in #discord-bots.
ight
I hadn't seen this before. Thanks, I hate it.
you're welcome❤️
I get the rationale. I'd probably find more uses for it than I find for Literal. But yeah, I don't like it.
I don't understand why there are multiple type linting packages
I don't get how one can be different from another from a technical level
at this point, I don't get why someone doesn't make a typescript-esque thing for Python
There are different approaches to type inference
What would that do
normal typecheckers haven't reached that stage yet
what stage?
where you can have typechecking that actually works well
you don't like mypy?
what do you miss in it?
knowing what my types are.
Why don't you know what the types are? Are you adding annotations?
the frameworks I depend on don't
honestly the type inference is kinda bad
but adding all the imports from typing makes it too verbose
relevant xkcd
Because the PEPs don't specify the behaviour (the "type system") exactly, so type checkers have their own opinion on stuff.
For example, mypy thinks that all callable attributes of a class must have a self argument https://mypy-play.net/?mypy=latest&python=3.9&gist=dece2a49e22531eded9b72ea569c94e0, which is weird and unexpected IMO
Another example is that pyright supports recursive type aliases (like JSON = Union[dict[str, "JSON"], list["JSON"], int, str, bool, None]), and mypy doesn't.
Another area is type narrowing. AFAICT, type checkers do it very differently.
I agree that it would be better to create a "single source of truth" for typechecking, just like there is CPython as a reference implementation. But the type hinting system is still in rapid development, and people are disagreeing on it all over the place.
And then there's 10000 features that only 1% of people need, so they don't get implemented
Hi could anyone help me with a webserver i am writting
@unkempt rock This is a discussion channel, not a channel for general help. You could check out #❓|how-to-get-help or #web-development.
Could i discuss my project then
@unkempt rock This channel is for discussing the Python language itself and its ecosystem in general. If you want to get a review on the structure of your project, #software-architecture is more appropriate. If you want help on solving some issue you have in your code, see #❓|how-to-get-help or #web-development.
AND no type checkers properly support descriptors
They also don't support getatrr and setattr
that's pretty difficult
types aren't simple
and, to use that example, TypeScript itself also has weird problems
yes, ts does have problems, but it's still better than plain js
Python 3.6 issue when using lstrip on character ending with _s
expected result should be state
You're misunderstanding the chars argument of lstrip.
@woeful sinew
Optional. String specifying the set of characters to be removed. If omitted or None, the chars argument defaults to removing whitespace. The chars argument is not a prefix; rather, all combinations of its values are stripped.
The chars argument is not a prefix; rather, all combinations of its values are stripped.
I guess i'll be using "...".replace() insetead. and yes huge misunderstanding
this is a channel for discussion of the language btw
Why do some programs use Queue.put(item) and then process the item from that queue in a thread, rather than just do ThreadPoolExecutor.submit(process_item, item)? ThreadPoolExecutor.submit does not block. So can we just skip using the queue to store the items? I do not know how this works, so I would appreciate if someone can help me understand
idt queue.put blocks either
Is it not an extra step?
but queue + threading is more lower level compared to concurrent.futures
threadpoolexecutor uses a queue internally for tasks anyways
This makes sense to me. Now I get it
Thanks 🙂
I have talked to a few different people and everyone seems to have different opinions, so I was wondering if anyone could weigh in with their preferences and experience. Lets say a system that reads json from an external API, the data needs to be transformed into a generic "internal" structure and passed on to either other systems or saved to a db. Would you rather transform the data into a new dict with your key names, or would you use several dataclasses for that. Lets say that the json is fairly nested and represents several related objects with a few levels deep hierarchy.
your question would probably fit better in #software-architecture
this is a channel for discussion of the language itself
it's kind of a data engineering problem, so you could try #data-science-and-ml too
Will do, thanks
:= is called the walrus operator in python. why not name it fangs ??
because fangs are not that long, walruses are cute and their faces are literally only eyes and their tusks...? 😉
you've.. never seen a walrus?
looks horrifying
will python have a native compiler?
no
It is highly unlikely that python will ever be able to be compiled to complete native code
ok
Have you seen Nuitka? https://github.com/Nuitka/Nuitka
does cython qualify?
key word is native here
Nuitka translates the Python modules into a C level program that then uses libpython and static C files of its own to execute in the same way as CPython does.
Is this different than Cython?
Well, Java and C# can't produce native executables (not requiring a runtime) as far as I know, and they're fine with that.
Unless by 'native compiler' you mean 'compiler that ships with CPython'
this is what i meant
ok
not what I thought you meant, either.
I think there's about zero chance of that ever happening. There's no reason why the CPython devs would ever want to bundle such a thing with the CPython interpreter, and there's no reason why the devs who work on such things would want to live with the loss of control over their release schedule, support schedule, copyright assignments, etc that would come with handing it over to the CPython owners
and wanting to compile to native machine code is a niche use case
And the CPython developers probably want to keep developing CPython, rather than building an alternative or replacement.

I was a bit surprised to learn about the mutable default parameter, have you guys ever found a use for it?
>>> def b(a=[]):
a.append(1)
print(a)
>>> b()
[1]
>>> b()
[1, 1]
>>> b()
[1, 1, 1]
Maybe it's just a quirk of having a mutable object as a default parameter.
the "normal" use of it is to cache
In [1]: def f(x, cache={ }):
...: if x in cache:
...: print(f"Cached value: {cache[x]}")
...: else:
...: cache[x] = x
...: print(f"Non-cached value: {x}")
...:
In [2]: f(1)
Non-cached value: 1
In [3]: f(1)
Cached value: 1
of course, there's a common decorator for caching function calls now, so this idiom is less common
There's no difference between py def b(a=[]): a.append(1) print(a) and py default_a = [] def b(a=default_a): a.append(1) print(a) del default_a (assuming the name default_a didn't already exist, anyway)
that is: default values for a parameter aren't evaluated when the function is called; they're references to objects that are saved when the function is defined.
in fact, you can even see that it works this way.
!e ```py
default_a = []
def foo(a=default_a):
a.append(1)
print(a)
foo()
print(default_a)
print(foo.defaults[0] is default_a)
@raven ridge :white_check_mark: Your eval job has completed with return code 0.
001 | [1]
002 | [1]
003 | True
yes, I think that's what it is, there is no special behaviour
I don't think I've ever seriously used a mutable default, if you want to take e.g. a list and default it to an empty one (and not use None as a temp value), I find that you can usually default it to e.g. an empty tuple instead, and relax the annotation to a Sequence or Collection, depending on how you use it
not always, but usually I find that I don't actually really need it to be a list
some of my colleagues think that the annotation should be as lax as possible, and that annotating something as a list when it really only needs to be an iterable is a lie
Yes. It actually has an impact, because list is invariant in its type parameter, and Iterable&Sequence are covariant.
If you're using a typechecker, it won't let you pass you list[bool] where list[int] is expected (because the function can append 42 to it, and it's no longer a list of bools).
Even if you're not using a typechecker, it will hinder your reasoning about the function.
well, not lax, but rather the most precise -- you should annotate the interface you need, but no more
(sorry for nitpicking!)
yea, but I think there's also a practical point of view, where asking for a list always means that you can start using it as such without breaking existing code calling your function
@sacred tinsel why would annotating it with Sequence break existing code?
well you may run into a situation where you want to pass the arg to another function that does want a list, for example (even if it doesn't really need one)
and in that situation you have to start thinking about what needs to change where, so that the annotations are compliant everywhere
hm, yes, that makes sense
you could pass it a list(sequence), but that's extra overhead, and maybe a bit confusing
but I do very much agree with this
you should annotate the interface you need, but no more
I think it's called 'interface segregation principle' or something like that
well, there's also a noise aspect to this.
When you start putting in MutableMapping, Protocol, TypeVars in, it can become a huge mess
yeah and I think for various people the threshold for when it becomes noise (the practicality of just using something everyone knows vs the purity of theoretical correctness) is in very different places
e.g. Dict[str, Dict[int, bool]] is nicer than MutableMapping[str, MutableMapping[abc.Integrel, bool]]
Overgeneral functions can make it hard to understand what a function does.
I'm learning PureScript, and I was a bit scared by this function:
any :: forall f b a. Foldable f => HeytingAlgebra b => (a -> b) -> f a -> b
Most of the time, the function will work like one of
any :: forall a. (a -> Boolean) -> Array a -> Boolean
any :: forall a. (a -> Boolean) -> List a -> Boolean
^ and now it's more or less clear what it does
but sometimes it makes it clearer, e.g. if a function expects Sequence[int], it's pretty clear that it doesn't mutate the list you pass in.
I can barely read the second example 😫
any :: forall a. (a -> Boolean) -> Array a -> Boolean
-- | | | |
-- this is a generic function, with a generic type `a`
-- | | |
-- first argument is a function (a -> Boolean)
-- | |
-- second argument is an array of `a`s
-- |
-- function returns a Boolean
basically
A = TypeVar("A")
def any(fn: Callable[[A], bool], Array[A]) -> bool:
...
(works exactly like the built-in the function checks if any element satisfies a functionany)
okay yea this makes sense now
well seeing the first signature would definitely have me googling Foldable and HeytingAlgebra even if I had an easier time reading the syntax
@sacred tinsel correction: it's more like any after map (seeing if any value in an array satisfies some condition)
Well, the main point is that this is an example of how you can overcomplicate things. It's technically the right interface, but at what cost
I guess you could compromise by including a very concrete example in a docstring:
A = TypeVar("A")
B = TypeVar("B", bound=HeytingAlgebra)
def any(fn: Callable[[A], B], Iterable[A]) -> B:
"""
>>> any(lambda x: x > 0, [-1, 0, 1, -42])
True
>>> any(lambda x: x > 0, {-1, 0, -42})
False
>>>
"""
...
yeah, although assuming that's something like an stdlib function or a general utility, allowing it to be used in the most broad variety of cases makes sense
sometimes you can be more or less certain that your function will only be called from a specific context, and then it's easier to justify just going for the simplest interface
Anybody ever heard of descriptors 
I was just playing with python and made this code on my pc and ran it and I saw something wierd while the program was printing all of these numbers I couldn't go back since the program was running so I pasted the code in Programiz to find that there's a bug that happened without any touch of the user, it seems that there's a very small percentage for this to happen but it does. so why is that?
that looks like a bug in whatever program you're using to run the Python code to me - try with IDLE or a regular Python shell and it probably won't occur
I think Programiz ate two \n without displaying them, which would be a bug in Programiz
it could be that when output is being produced faster than it can keep up, it skips some?
@limpid forum In response to the fangs operator. I don't think any aspect of a walrus is reflective of the python language. (blubbery + slow != python ) well maybe the slow part 😆
In addition, the name walrus operator doesn't fit the actual symbol. this looks more like a walrus o= or beaver 😆
: are eyes?
Yup, what hmmm said. Walrus's face is basically eyes + tusks, so := makes sense. It also introduces another animal besides Python 😛
Remember that Python itself is named after Monty Python, so a bit of goofiness in the naming is in the spirit of the language
hm, I also googled "walrus emote" and it seems := with sometimes additional characters have been used as text emoticon for longer than Python's walrus operator
@sacred yew :0= perhaps
one of the examples of walrus emote I saw was (:=), but there were also :3= and similar ones. I already closed my search results but I think something like :0= might have been there
:3= 👍
when smart people run off the rails: https://groups.google.com/g/comp.lang.python/c/wU4PNtHu13g
("New Python Implementation", which is complete nonsense vaporware, followed up with insults)
i would love to find out what that guy is actually thinking, but it's impossible.
I am also creating Ada and Haskell implementations which have a similar requirement
I refuse to believe what I'm reading but if said person claims that their universal compiler is able to compete with the likes of GHC, then I'm not sure they understand the implications of going head-to-head with 30-year-old implementations with hundreds and hundreds of contributors and various white paper publications for the case of GHC and just FP in general
I think they do have a point about the lack of a standard being a bit detrimental to Python.
I've never really heard of universal compilers though.
https://github.com/i42output/neos I believe this is their repo.
@limpid marten how is the lack of a standard hurting Python?
did you see the later posts about c needing standards because multiple implementations were disagreeing? Does Python have that problem?
I can't help but feel the lacking of a standard and thus the lacking of other mature implementations that aren't just imitators hurts the language.
pypy is the most mature of the other implementations - in what sense is it an imitator?
PyPy3 implements the Python language version 3.7.9. It has been released, but Python is a large language and it is quite possible that a few things are missing.
The fact they can't definitively say it implements Python 3.7.9; which would be possible to say if there existed a Standard means it's simply just an imitation, but I could be misunderstanding.
which would be possible to say if there existed a Standard
I don't think that conclusion follows
If there were a standard, it would be quite a large standard, and it would still be possible that a few things are missing.
It would be much easier to design an implementation given a standard rather than implementing behavior and comparing it against an original implementation.
As other compliant implementations like PyPy, IronPython and Jython find areas they have a problem with, they seek clarification from python-dev as necessary (the C API, reference counting and the GIL are all CPython implementation details, for example).
Whereas they could otherwise access a standard being the source of truth, and not rely on some dev channel.
jeeeez, that list of languages looks like a nightmare to pull off
It would be much easier to design a language given a standard rather than implementing behavior and comparing it against an original implementation.
I'm not sure that's true. For instance... the last C++20 draft standard was 1809 pages long. And Python's would almost certainly be much, much larger than that - Python is a bigger language, with a much, much bigger standard library.
and... where the standard is ambiguously worded, or unclear, people would still need to check what CPython does or risk incompatibilities.
I daresay that reading a standard to see how something is supposed to behave is likely much harder than just trying it against a reference implementation - you may not find all the edge cases that way, but you don't need to have all the edge cases covered from the start - if someone finds something you missed, accept a bug report and fix it.
and it's not clear that you'd find all of the edge cases by reading a spec, either. specs are really, really hard to read.
https://doc.pypy.org/en/latest/cpython_differences.html Just and interesting read on how the difference in memory management between CPython and PyPy changes behavior.
I'd really like to see a real non-contrived Python program that behaves differently in both implementations. It seems weird to rely on some of the different behaviors.
I suspect most of those would still exist even if there was a standard. The standard would specify most of garbage collection as implementation-defined behavior, so as not to exclude creating things like Jython and IronPython that directly leverage the JVM or CLR GCs.
I don't think so, the weirdness on how many times __del__ is called would certainly be something standardized wouldn't it?
But anyway, just an interesting thought. I've always found it weird that CPython doesn't have a JIT.
Has there just never really been a need for Python to be fast by default?
Nope - that's already specified as implementation defined in the Python Language Reference, which is the closest thing to a spec that exists today. https://docs.python.org/3/reference/datamodel.html#object.__del__
It is possible (though not recommended!) for the
__del__()method to postpone destruction of the instance by creating a new reference to it. This is called object resurrection. It is implementation-dependent whether__del__()is called a second time when a resurrected object is about to be destroyed; the current CPython implementation only calls it once.
Despite it saying it is only called once, the PyPy differences page I linked mentions how this is false and there exist counter examples.
well, you can explicitly delegate computation to extension modules
that's a failure of documentation, then - but the docs explicitly say that it's implementation-dependent how many times it will be called.
on the other hand JS can’t because it runs in the browser
That's a really good point.
just read the whole thread, it seems like, at least according to Ned Batchelder, that Fibble has been claiming to have been doing this for at least 2 years, which seems backed up by the git repo's commit history, as 166 of the 171 commits were 2+ years ago. as such, there seems to have been a long period of inactivity on the repo, so I think that the OP was/is trolling, because I'd think something so important to him would be actively maintained or something
I can't help but feed that nedbat, and Ned Batchelder may be the same person.
indeed they are.
wait shoot
TIL. I feel when it comes to C++ I have to understand how more things work, but ye did mention stdlib so I guess there's more going on
not sure how to fully express my thoughts rn 🙈
true, but you're a language user, not a language implementer. Python has higher level features that frequently make it easier to use than C++, but all of those high level features would need to be fully specified in order for people to rely exclusively on a spec when making alternative implementations
Is there a resource for idiomatic advanced python for experienced devs transitioning from other languages?
I liked some of Hettinger's talks, but he only has so many of those
Is there any reason that all the function-like types don't have a class hierarchy?
I'm having trouble parsing that question - what do you mean?
>>> types.FunctionType.__mro__
(<class 'function'>, <class 'object'>)
>>> types.MethodType.__mro__
(<class 'method'>, <class 'object'>)
why is MethodType not a subclass of FunctionType?
My question was inspired by this: #help-corn message
!e
import typing as t
import types
print(isinstance(types.FunctionType, t.Callable))
@boreal umbra :white_check_mark: Your eval job has completed with return code 0.
True
hmm I guess there's that.
a function is one type of callable, a method is a different type of callable (conceptually, a method is a lot like a partial that binds a self arg to a function).
they're not, though - it's composition, not inheritance.
a method has-a function and an object
not entirely sure what distinguishes a method from a "function with an object in its closure"
it could be implemented that way, and thus be a subclass, but it isn't, so it isn't.
i mean, the question was why its not
the inheritance hierarchy accurately reflects the implementation. MethodType is not a subclass of FunctionType because a method is not an instance of function
i would be more accepting of the hierarchy if we stuck to the "functions are pure", "methods can have side effects" thing
but alas python doesn't do that
I can only imagine that eventually it becomes reading existing codebases/docs
Some things there might not be up to date perhaps but looks like it covers what you need somewhat
what language uses this definition?
don't most languages use "methods are functions attached to classes"
I guess this pertains to a practice and not an actual language specification; the notion of strictly keeping state to manageable objects arises pretty quickly
Likewise, that also means limiting the state available to top-level functions, unless you're doing implicit state for a DSL like matplotlib, but I don't see how that relates to the hierarchy of callables in Python
Duck typing works extremely well with Python, in this certain case
The sad thing is, I don't think this is a pure troll. I think he genuinely believes what he is saying. But he's either got no idea what's involved, or will build something that implements 5% of a Python-looking thing, and then claim that the rest is irrelevant. But his style is so confrontational, it's impossible to either educate him or find out what he's thinking.
The neos universal compiler itself is language agnostic: a pre-requisite for the requirement to be able to compile any programming language.
That sounds like the worst way to make a compiler.
Use the same compiler to compile Rust, Python, and Haskell? They must be joking
I had some long discussions with him in the ##programming IRC channel before he was banned there. He seemed uninterested in the differences between Java classes and Python classes.
I don't know if it's ok to say this but I recently started learning python and have no friends to discuss with. Anyone willing add me as a friend and shoot me a text.
Mentors and study buddies are both welcome
isn't the difference between python classes and java classes are that python's classes are just sugar for a dictionary.
python classes are not sugar for dicts
attribute access is not analogous to dict lookup
I think what I meant was, at the implementation level, I'm pretty sure it's just a hashmap
you can even have a class without a backing dict at all, such as int, str, Function
they do use a dict to store attributes
if you specifically define them from inside python
yea so I mean, that's not really close to how java treats classes
Any slotted class, specially, doesn't have a dict
Or is it instances of slotted classes?
ye, but it is not the difference. The difference is more in that java classes do differentiate between fields and methods, have compile time deterministic attribute access, and are overall less flexible and more safe. And that is disregarding the more minor details like multiple inheritance.
the class has a mappingproxy under __dict__, which generally means there is no dict, and the instance doesn't get a dict at all
right but that different comes from how Java leans heavily towards OOP wereas python leans kind of... In the middle of everything
everybody hashes something at some point guess that was a bad point
python is closer to OOP as described by the original smalltalk
java is the deviant one, it just turns out to be the better approach years ago.
realistically though, they both suffer from their design decisions.
well, there's no perfectly designed language
yea for sure
honestly, I do like python classes
I really don't, I mean they are a convenient way to bundle code, but the FP approach is kind of more enticing
they don't offer a whole lot except some code reuse
Well, you'll still need to use classes in one way or another to define types (e.g. with dataclass).
just in a stripped way, I guess
and honestly, if you want to do functional programming, Python is probably not the best language
yea it's really not that great for FP nor for OOP really - not the worst though!
there aren't a lot of strong FP langs out there 😦
haskell is the only one you really need tbh
yea but haskell is more than a can of worms to take into production
that's something python did get right
you guys a good example snippet of a functional programming in haskell. I see people talk about how awesome it is but i can never think up a pattern to use it
sure I've written some haskell 1 second
its just another way to think about organizing code. typically incredibly expressive.
classes do have some advantages over FP discriminated unions, FP doesn't handle "the user needs to provide these functions, some of which are optional" very well, which turns out to be very common. I do not consider constructors that have 8 variants each of which takes a different amount of functions a great solution to the problem. Typeclasses do solve this to an extent, but they do have their issues as well.
I really like the way julia handles its type system and balances FP + imperitive paradigms
Man. Fucking multiple dispatch looks enticing
Like it just seems so clean, even if its a bit more verbose
you mean, pattern matching?
multidispatch is seductive for sure, it makes a ton of sense once you use it.
it runs fast as hell too
No I mean multiple dispatching
Where you write multiple functions, all same name, indicating the type of each argument and the language picks the appropriate one at run time
haskell doesn't have multiple dispatch, it would not do well with the type system
its pretty glorious
yea that'd be a nightmare lol
Python has a multiple dispatch library, and it has singledispatch built into functools?
haskell does have algebraic types and a lot of other fancy stuff though
it is really neat in Julia, true
No, typeclasses pick the appropriate function at compile time
yea but you don't really get anything out of it. Pythons performance is pretty rough
Yeah, you get the organizational part but not the speed
I'm a fan of some of the trait systems that have been showing up. Like in Rust
to be fair, most statically typed languages do have multiple dispatch, C++, Raku, Nim, Java, Haxe...
Nim took out MD I guess.
@swift imp If you just want a random snippet, I have used haskell for doing animations recently
https://gist.github.com/decorator-factory/35971c191302052a5217c1e5ff8ed375
it's certainly a niche application (and the code looks nothing like normal haskell) , but a cool one
C++ is pretty awesome though, I wish I knew some of the more modern stuff
thanks
FP for event driven animations and things like that is super neat
This project: https://github.com/gurkult/py-gurklang (on which mostly I and lakmatiol are working on) uses functional ideas to some extent, but uses a more imperative core for the main tight loop
It's a... functional stack-based language
interesting
yea for python you don't wanna fuss with FP loop controls anyways
there's no TCO I don't think in python lol
no, there isn't
but yea having the choice between FP and Imperitive has proven to be really powerful
TCO?
but you could make a sort-of TCO where you return a discriminated union of Return(value) or Next(*args, **kwargs), which is really what a tail-recursive function is.
and then run it in a while loop
but that's weird
Quite interestingly, the language implementaiton here is stackless, i.e. it doesn't use Python's call stack to do nested calls in the language (otherwise it wouldn't work).
tail call optimization
how are you doing it?
JS?
It's a bit of a mess with spaghetti: https://github.com/gurkult/py-gurklang/blob/master/gurklang/vm.py#L52
Basically, I have a "pipeline" of instructions that are executed one-by-one. When I see a function, I just put all of its instructions on the pipeline. If the function needs a local scope, it's created from the current one (and a scope-popping instruction is put at the end).
So functions that don't require a local scope (since it's a stack-based language, so you can do {2 + 3 *} or something like that) can be run in constant stack-space; and all functions in general (even with local scopes) don't have a stack limit.
Right now these scopeless functions are not automatically detected, so you have to tell that to the interpreter. But this will probably be fixed in the future.
TCO: for example, scheme
(lambda (fac n acc)
(if (< n 2)
acc
(fac (- n 1) (* n acc))))
``` will convert this into a while loop akin to
```py
def fac(n, acc):
while n >= 2:
n, acc = n - 1, n * acc
return acc
```, which means that you can't get a stack overflow, which is very possible with normal recursion. This conversion, although always possible, is only trivial in cases where the result is directly another call to the function, rather than some expression containing it.
interesting
You probably meant fac instead of fib 🙂
I don't blame ya lamatiol for recursion the 2 gotos are fac or fib lol
there is also the much cooler fib version which uses the halving formula, but I do not have that one memorized.
ah yea I had to do something like that for an interview a few years back.
It should be pretty easy to derive from the matrix power method
I have a slightly weird question - not intended to be abrasive
Given the 5min conversation we just had, I've determined you all are smart. I spent the last few months scouring programming languages to help me out at work, and really ended up pivoting pretty far away from Python. What draws you in to using python?
I don't ever have to drop a project because there is no library for what I want to do.
that really annoyed me about nim
oh yea nim is super young
isn't nim like, pretty old
if it is old, it feels new
nim is 2008
wow thats pretty old!
and I don't have to wait 10s for every code change, that really annoyed me about java, even with hotspwapping
yea Java has slow compile times. I've had scala projects take 20min to compile on a good day
every compiled language has slow compiles, except like... C in debug mode and even then only on a small project
yea Go has really fast compile times, but it's end performance isn't amazing, and the language itself is very lean.
hmm, never tried Go
honestly, it's sugar is pretty meh, I like Rust a lot more. But - Rust can be slow to compile for large projects
If I'm doing mathy stuff I find myself using Julia, if I'm doing systems stuff C++(but now learning Rust still super new to it though)
yeah, I can agree with that. Another thing I like in python is that things focus on being simple, unlike something like Rust. I know there is merit to the complexity in rust, but it doesn't make it any less complex. I would prefer a static type system though, but I don't think there is a simple type system which is useful and doesn't get in your way.
Can you guys think up a good overloading using for @
salt-die's email class
yea I can appreciate that. The language is overall very simple to use.
ye, I can't imagine a total beginner to programming starting with Rust or Haskell
matrix multiplication, function composition are two main ones I can think of for @
yea that would be a total mistake.
a non standard one lol
hello there cupcake
So the email class is def interesting
hi nekoboi
;)
Haskell is sort of possible, since you can just ignore some of the complexity and use ghci for everything
i think i could use haskell with a beginner
I feel like a CAML would be better to see if they lean more towards imperitive or FP
FP to some people just clicks
scheme is IMO the best lang for beginner FP
never tried scheme, is it a LISP?
see I would lean more toward julia probably just because LISP's can be a little hairy to read. But, never tried it so I can't say
hOw Do YoU mAnAgE tHoSe PaReNtHeSeS
julia isn't a lisp but it has a lot of lispy stuff in it
LISPs are pretty readable once you get used to it
F# is basically a ML
hmm, this is getting a bit off topic, lets move to #ot2-never-nester’s-nightmare
sure 😄
@unkempt rock when i mentioned the difference between java classes and python classes, i was thinking of things like __str__, or multiple inheritance, or @property
__str__ is almost the exact same semantics as .toString()
"almost"
@flat gazelle but even so, if you want to implement Python, somewhere you need to talk about a method called __str__
@flat gazelle the implementor in question is talking about "implementing" Python with a declarative json file.
wha-
I mean, xslt does work and is turing complete
don't see why json couldn't have such a schema
well, of course you can implement Python like that
just like you can implement it in brainfuck
it does seem like a slightly superfluous idea, considering that you could just use a programming language instead
@flat gazelle i think their idea is that once you have a large collection of programming language parts, a json file can pick and choose which ones to put togethr to compile a language.
that still just seems worse than writing a compiler in a programming language
JSON is not a great config format
yeah, especially considering that e.g. Python and Haskell have completely different execution models
and different GC algorithms if I understand correctly
and how is he going to encode different type systems? (e.g. Agda vs Rust vs C++)
as far as I'm concerned, it's all nonsense, with a thick wall of aggressive insulting to defend it.
if it's just a JIT compiler for dynamically-typed interpreted languages like Python or Javascript or Smalltalk, then maybe it's not that crazy. But they're still different, so I can't imagine how it would work
"it can compile any language"
and also be faster than CPython
I am not doubting this is possible, a JSON schema that is designed to emit LLVM IR would definitely be possible, and neos just seems like LLVM as far as this project is concerned. It doesn't seem at all worth the effort
Apparently it's enough to write a language inside a schema file and it will compile it. 
I mean, what is a C++ compiler source code but a schema
my key issue with this is really just that I don't want to write a JSON schema as a compiler, since JSON is not a good format to write with your own 2 hands
next step: make a json generator...
@flat gazelle to be fair, he says he is using "relaxed json", so you get to have trailing commas and comments
json5 is a thing indeed
I feel like the only config powerful enough to make this not boilerplate is dhall
ugh, another one?
hahaha
https://dhall-lang.org/
at least it compiles to a bunch of config languages like JSON/YAML
it's the language PureScript uses by default for configs
but maybe we have enough config languages already
sorry, but if a syntax designer puts commas at the start of the line, they have already surrendered
you don't have to, it's just a common style in functional languages that dhall was inspired by
right. it's absurd, and unneeded heree.
leading commas are only to work around the lack of trailing commas.
so why do it here?
i have no idea, it does look strange
I think it's to make it hard to forget one
no, it's to make lines diff-able in languages that don't allow trailing commas
for some reason JSON doesn't do that, which seems strange to me
JSON doesn't allow it because JS didn't used to, and to be strict.
I meant that people write JSON without leading commas to improve diffs
and I wonder why
i see
if all languages allowed trailing commas, no one would have used leading commas
Maybe I should ask about dhall in an FP discord
@spark magnet
I guess dhall has some reason to use leading commas, since it allows a leading leading comma for better alignment
{
, foo = "a"
, bar = "b"
}
it's just a bad habit. you can put the commas at the ends of the line
or at the starts
I am guessing it is to be consistent with things like
name
| True = 4
| False = 5
```, since FP does have leading operators in quite a few cases
why are trailing commas better?
I guess it's just more consistent with human writing: "a, b, c"
but then, we have bullet lists, where the 'delimiter' comes first
- a
- b
- c
and also: You choose between a) broccoli b) salad
honestly, I feel like configuration languages should be whitespace aware
yes, because it's more consistent with usual writing
but in a config, it's more like a bullet list, isn't it?
yes
like, isn't
{
foo = "a"
bar = "b"
}
``` better overall
actually, in YAML a list is marked with -, like a bullet-point list
yeah, this is also better because you can't forget a comma/bullet point
that was actually a pain point quite a few people had with dhall, since it gets nested easily, you can easily lose track of the many many different kinds of closing braces.
@spark magnet Do leading commas really surprise you more than a config language having a type system? 🙂
in real text, you never write comma separated lists on multiple lines. I don't think I would ever find this in a real text. Everyone would use bullet points or numbered lists
@grave jolt oh, there's plenty i'm concerned about in dhall-lang 🙂
it's just that having the very first thing you see use quirky syntax was a red flag
@flat gazelle but also in real text, you would never put space between a thing and the comma following the thing
it's idiomatic in some regions
admittedly not many
it is, but only because trailing commas are forbidden (or were) in those regions (assuming by regions you meant programming language subcultures)
wait, when you said "real text"
I took it to mean "natural language"
if that wasn't your intention then my bad
I meant geographical regions
no, you are right, we meant natural language
yeah, a fair number of people actually have a space before the comma and not after
which is a bit 🥴 to me but well
quite a minor point though
yeah ,that's pretty uncanny ,at least for me
STOP 😡
okay ,no more ,sorry
🤢
I promise , don't be mad , calm down
Even if you do use bullet lists, trailing commas on each line are grammatically correct. https://www.dailywritingtips.com/a-guide-to-vertical-lists/
grammatically correct doesn't mean correct though
bottom line: if every language allowed trailing commas, no one would ever have started using leading commas
What are we discussing
I think that's true.
Conventions where you start a line with a comma. Like the syntax example on https://dhall-lang.org/
I've seen it mostly in SQL queries, which also don't allow trailing commas in lists
also, that snippet, starting the contents of the {} on the line where you put the {, but not ending the contents on the line with the }, it just looks lopsided
I think types and functions like that might be more useful in some markup language?
I made a small language that explored this a bit https://github.com/decorator-factory/python-fnl Not in any static-analysis way, but kinda works.
Basically, in HTML you can't put a "block" element inside an "inline" element, e.g. a <div> container inside a <b> (bold), otherwise you get undefined/quirky behaviour. And this checks that it doesn't happen.
But in theory, it can do a bit more
I don't really like YAML. It's a bit confusing to me that you have different ways to define stuff as well.
there's also RON https://github.com/ron-rs/ron -- yet another config language, this time without types and functions
Anyone think it's a shame bindings can't have docstrings?
Like a variable can't have one
the name is the docstring /s
docstrings are, and have always been, a hack. It's cute, and succinct, and it works in the 99% case, but using string literals for documentation can't generalize to describing variables/attributes
and so most of the dissent here comes from the fact that docstrings are good enough for functions/classes/modules, and that anything that works for variables/attributes will necessarily look very different from docstrings.
That said, Annotated might be the eventual solution for this
guys i wanted to start learning pygame
can i get some suggestions of how to start?
Hey @lime walrus, it's probably better to ask your question in a channel like #game-development or #python-discussion. We try to keep this channel reserved for more advanced discussion of the Python programming language itself, as we want to give those kind of conversations a place in our community too.
my bad @wide shuttle thanks btw
If it makes you feel better, Lisp does it too. It's more a clever use of syntax than a hack. Also Sphinx supports using docstrings to annotate bindings and even type annotation declarations that don't have a RHS assignment
@swift imp ^
x: int
"Sphinx interprets this as a docstring"
Introspection stands in the way of attributes and names ever getting proper support for docstrings. Docstrings result in __doc__ being set, which means that they're only possible in places where new objects are created. You can never have a real docstring on something like x = 1 because there's no place to put the __doc__. So in that sense Sphinx recognizing docstrings on name bindings is even more of a hack. It's assigning meaning to them by parsing the code by itself and investigating the AST, not by introspecting the objects the way docstrings are meant to work. Which is why it's the only tool I know of to support that.
Well then where are annotations held for a variable?
I know variables are just names and lack a dict but something is storing the annotation isn't it?
They aren't. They can't be, for the same reason as docstrings can't be. There's no place where the annotation could be put.
In bar: int = foo there's no new object being created, so there's no possible way to introspect the int annotation from bar, because instead you'd just find whatever annotation foo has.
you can get int from the __annotations__ dict
They're definitely stored
in theory, bar: (int, "the bar attribute") = foo is possible
Huh, I stand corrected. So they're stored in a special property on the namespace the assignment happens in.
I did not realize __annotations__ existed even in globals
>>> number: int = 5
>>> __annotations__
{'number': <class 'int'>}
only ever accessed it on a class
So like in theory if python had an eol character, typical docstring convention could work for variables and attributes lol
Ok color me this
ye, it exists on modules too
If I have an annotated variable in module x and import that variable from x into y, does the annotation hold
well, the annotation is stored in the module x
In other words, no.
I'm wondering if the import system causes it to move it into module y
so once you alias to a new variable in a different module, it gets lost unless the typechecker keeps it
I would be very surprised if it got copied
In which case importing a variable would cause it to lose its docstring
But I guess you're right that it would be possible to actually support docstrings for bindings by creating a new special variable like __docstrings__ for holding them. With the caveat that it wouldn't follow the object around, it would be stuck to the namespace.
That would be partial support, but enough to make it work in the context of ```py
import module
help(module)
Yeah
I mean would copying them really matter?
Would it be that costly?
And then if you use like as to alias it can use that new alias instead
I have no idea how to do that at the c level, I'm just saying
Doesn't seem that that difficult
That would make py from mod import foo do something different than ```py
import mod
foo = mod.foo
How so?
foo = mod.foo would be a lot more difficult to get to copy
it's more a matter of = takes a value on the rhs and never gets to touch the expression ast
the more stupid solution is to add a __doc__ next to the refcount in the pyobject struct
Neither of those work, because the same object can have different annotations and different docstrings for each reference
oh yeah
that would require boxed references, rather than just raw pointers
which is a bit too silly for me
I think the problem is that when you do the assignment or import, the annotation and doc is technically on the lhs
And it's all fucked
min: int = 0
"The minimum value ever read"
average: float = min
"The average of all values read"
``` There's only one `0` object there, and it can't hold both annotations and both docstrings as properties.
Yup
how to get connection uri for a dtabase?
Anyone doing the Pirple python course here?
I'm dynamically creating classes for some tests using type(name, bases, clsdict). How do I specify the metaclass though? I tried setting __metaclass__ in the clsdict but it doesn't seem to work.
I'd use types.new_class
But to answer your question, metaclass would be used instead of type
Right: instead of calling type, you call the metaclass (which is itself a subclass of type)
@peak spoke @raven ridge Makes sense. It did cross my mind, but for some reason I didn't try it at first.
TIL about types.new_class - I hadn't seen that before.
where can i learn python i want to learn
!resources
The Resources page on our website contains a list of hand-selected learning resources that we regularly recommend to both beginners and experts.
thanks
so is pywinauto what most people use for interacting with mouse/keyboard inputs?
I'm new and it just seems like I am guessing on how to fix my own code and eventually get it to work... I just don't know if its due to lack of experience or that the old code that is referenced doesn't work anymore.
What type of real life problems you face which you think can be tackled digitally ?
This isn't really the place for this discussion, but what I'd recommend is reading the pywinauto source yourself to see how to use it if you're finding bad references online. I don't use it though, I usually just write the binding myself with ctypes and using SendInput from the Win32 API.
hi does anyone know django rest framework
hi, i need to practice beautifulsoup, anyone have any tutorial
@unkempt roost @unkempt rock This channel is strictly for discussions about the Python language itself, not for general help with Python. You can look at the topical channels or see #❓|how-to-get-help.
I wonder if alot of software companies use pypy with python for performance boost
Hi- I have got python to read and name a specific excel cell that contains a URL(with openpyxl) , ALSO i have written a script with selenium to automate visit a website. How do i BRIDGE these 2 scenarios. And get SELENIUM to read that cell URL and visit the website automatically.(USING SUBLIME TEXT)
ok
Problem is pypy isn't compatible with Cpython apis, which make it less useful
i believe instagram uses pypy
Unless it changed recently, the few articles I stumbled upon from them were about cpython
huh, yeah, i can only find cpython articles
I reckon I should have implemented Python by the summer; just need to get my multi-precision arithmetic lib finished then I can start...
pypy actually does support a pretty large portion of the CPython C API - but it needs to do that through an emulation layer, which makes C extension modules built for pypy run much slower than if they were built for CPython.
balanced by the fact that CPython is so much slower than pypy 🙂
well... pypy is much faster at running Python code than CPython is, but CPython is much faster at running C extension modules than pypy is.
solution
make a third implementation with all the C extension speed of PyPy and the Python code speed of CPython
🚀
use a faster language if you need speed
something to think about when I create mine.
Now you have three competing implementations
There are much more. RustPython, GraalVM Python, maybe something else...
the true hallmark of a great dev is the willingness to create problems where none exist
But... performance sensitive Python modules are commonly written as CPython extension modules. So, counterintuitively, pypy speeds up the parts of a program that don't need to be fast, and makes the parts that do need to be fast slower. More or less.
I will have created one by the summer.
Are you planning to implement an entire Python interpreter from scratch?
not quite, I will be creating a Python compiler from scratch
||welcome||
lol
AAAAAAAAAAAAAAAAAAAA
Wouldn't a JIT/AOT compiler perform better for a dynamically typed language than a machine code compiler?..
the goal is for my implementation to be about 3x faster than pypy
!e Will it be fully feature compatible with Python? Will you be able to do things like:
foo = "10"
print(eval("fo" + "o"))
@raven ridge :white_check_mark: Your eval job has completed with return code 0.
10
@grave jolt my solution is JIT/AOT
but then it's not really a compiler, right?
I don't mean to be mean
but I'm getting flashbacks to that Google Groups link @raven ridge posted here
a few days ago
sorry 😔
just like pypy or node isn't a compiler
Ned posted, not me. 🙂
it compiles to byte code which is then JIT'd
hey how do i use excel cell value as input for driver.get(cellValue) to vist that url in cell. I have already imported the cell value and workbookwith openpyxl. cant find help anywhere
right, like pypy
@open acorn This channel is for discussions on Python. If you want general help, see #❓|how-to-get-help.
my python implementation will actually just be a schema file for my universal compiler
I think I am indeed getting flashbacks
are you making a compiler that would compile any programming language at all?
so it would be able to compile Agda, Python, and VHDL?
okay, VHDL is a stretch
But how are you going to configure different runtimes? e.g. Agda is lazy, and Python is strict; and Agda has quite a complicated type system
Ahhh, that kinda good and bad at the same time
@weary garden so how would you encode a type system in a schema?
switch to c++
Yeah - it supports enough of the CPython C API as of now that most extension modules can compile fine for pypy
by creating the relevenet semantic concept library(s)
relevent*
the schema in combination with semantic concept libraries defines the syntax and semantics of the programming language
just compiling Python isn't likely to make it much faster. The things that make Python slow to execute are all of the dynamic type checks and dynamic name lookups - which need to happen, unless you're implementing something that looks like Python but with different semantics.
Pypy latest version support up to python 3.7
You can make a faster language that looks Python-like, but there's only so much faster that you can make Python without making pretty drastic changes to its semantics.
And because everything is an object
pypy does it and I will do better than pypy
Even for basics types like int
well, like I was saying above - pypy does it, at the cost of being slower for the very things that people have optimized by writing them in C.
Which is what Cython and RPython does afaik
my solution: https://neos.dev
neos Universal Compiler and Bytecode JIT
By limiting the dynamic features, you get a superset/subset that's much easier to work with in terms of speed/optimization
I'm not sure about RPython, but yeah - one of the things that makes Cython faster is that it binds some names at compile time - you can't monkeypatch builtins in Cython modules.
though with Cython that's an option that can be disabled, for better Python compatibility.
/* Long (arbitrary precision) integer object interface */
extern typeobject Longtype;
#define is_longobject(op) ((op)->ob_type == &Longtype)
extern object *newlongobject PROTO((long));
extern long getlongvalue PROTO((object *));
one nice thing about open source is that it should be relatively easy to steal an arbitrary precision integer library, or a decimal floating point library, or whatever from another project.
I'm honestly not sure why people want to make Python faster. Python's language semantics is what mostly make it slow, as you said. Picking a faster language (C++/Rust/Haskell/C#/...) would dwarf any (relative) microoptimizations to the interpreter, right?
@raven ridge I am making my own.
micropython's int implementation is a bit easier to understand than CPython's, I think.
What is micropython
C++, C#, but rust lacks a big community, i dont know much about haskell other than the fact it is functional
For the most part, yeah - I'd expect so. I mean, pypy can be a lot faster than CPython in some cases, but a) it's slower for the things that people have already bothered to optimize, and b) it's still not as fast as just using C or C++ or Rust or whatever in the first place
But i would agree for rust being a good pick for performance
micropython is a Python implementation designed to run on resource constrained environments, like microcontrollers
Cool
At least python is the best for ML and data science
there's also a fork called circuitpython, maintained by adafruit, that is designed to be supportable across a wider variety of platforms.
Python gui programming isn't that bad, but there are better options
or - circuitpython has fewer features than micropython, but the features that it does have are implemented much more uniformly across all of the platforms it supports.
I think people say python is best for ML and data science simply because python is pervasive.
you will be able to visualize your python program as nodes and set breakpoints on nodes 😄
connected nodes*
I don't think it's pervasiveness, I think it's the robustness of the library ecosystem.
combined with the ease of getting started, perhaps.
How you will be able to visualize a Python program
you mean, like the cascading debugger in Thonny? 🙂
don't know what that is
it looks similar to pythontutor
Unless you're talking about visualising a syntax tree, not a running program, in which case I don't really see the point
yeah, pythontutor does that as well
(debugger/editor)
that debugger seems like by far the best thing about Thonny. It's an excellent way to teach people how stack frames work.
you will also be able to create GUIs like that from Python itself
you will also be able to mix C and Python in the same source file
you can already do that in Cython.
Can you give an example of how you would mix JavaScript, Agda and Forth in one file?
@grave jolt see https://neos.dev
neos Universal Compiler and Bytecode JIT
I've seen that
it shows you how you mix code
yeah, but those are languages with very similar semantics, right?
To pick from the language list, how would you mix Brainfuck and Haskell?
if there is no common semantic subset between the two languages when the code references each other then a compilation error will result
honestly, I don't see how this mixing could be useful
which is why the ability to mix isn't a functional requirement; it is just a side effect of my chosen architecture that you can do that
a mere curiosity
hmmm, i still don't see how you can mix languages though
will you be able to access variable and stuff cross-language?
yes
even though every language has a different type system?
as long as there is common semantics associated with the variable
hey, Haskell with PureScript should work!
minus the strictness and the concurrency model
*anything on the mentioned languages
ah
the convo earlier makes me curious, what exactly would it take to make a python "like" language fast?
nim is kinda Python-like, and statically transpiles to C.
never heard of nim before
there's only so fast you can make the language when everything is specified to be a dynamic lookup.
say, if i wanted to turn python into something that's fast, by forcing static types, but didn't want the added overhead of explicit declaration in terms of the code for example, that doable?
I've never written anything in it, but it looks quite nice
some kind of type inference
Type inference is pretty hard with an OOish type system, tho, right?
is it? doesn't c# have it now?
actually fundamental question, forgive my lack of knowledge here: how does a type inference system understand im trying to reassign a bad/different type in the code? Does it have to do essentially the same work that overhead of dynamic typing also underoes?
@weary garden you should spend more time implementing, and less time boasting.
someone on python-ideas had a suggestion for making Python faster by moving towards a two-phase execution model, with a separate "import time" that's distinguished from "runtime", and where certain dynamic operations - like rebinding a module level global - are allowed at "import time" but disallowed at "runtime", allowing you to cache the values of globals, and in theory making it easier to JIT calls to global functions.
i dont know, this is primarily an "outsider lookin in" perspective. but if you say that* it's hard with an OO type system then i'd be willing to even forego that. i suppose the thought experiment is this: what is the least amount of change a programmer might have to do such that their code still is as close to "python" as possible and this new "fast-python" language can achieve massive speedups?
And is this a futile endavour to begin with?
A language with type inference still has static typing:
int main(){
auto x = 42;
x = "foo";
}
so it will throw a compile-time error if it can't "unify" types.
oh okay, that's something
blah
@visual shadow type inference isn't an optimization technique, it's just a feature of static type systems where you don't have to specify the type yourself.
It's like solving an equation.
but knowing the types at runtime would allow you to do further optimization right?
known:
+ : int -> int
f x = 4 + x
f inferred as: int -> int
wdym by knowing the types at runtime?
so you don't have to check what types things are before performing operations on them?
basically im thinking type inference helps keep the cognitive load on a python programmer low, while enabling this "fast-python" language to work faster
so the inference is purely a "convenience" aspect i was going for
one thing i understand from the discussion so far is that in any fast language, dynamic typing has to go
Statically typed compiled languages don't do this checking at runtime at all. They know types at compile time, and that's why they're fast.
the biggest change you could make, with the most impact, would just be to make variable types "sticky". Once a value has been assigned to a variable, don't let anything else ever be assigned to it that would change its type, from the POV of the JIT. Or something like that.
I would love to ask questions about your ideas for this, but based on past experience, i fear it will turn contentious.
is it possible to make these sticky variables optimized for 2 types of data types without much overhead? trying to think of None and python's default params and so on now
@spark magnet maybe some other time; I am about to go to bed.
or let me rephrase that. A common construct in functions is having a None as a default value
Not without declaring the types, I suspect
where we check if its None before setting it to an additional value
i look forward to it.
hm okay
or perhaps by special casing None entirely, to force dynamic lookups if the value is the None singleton - the fact that it's a singleton may allow that.
i suppose we could introduce special syntax for specifically declaring default Nones
I just wonder why python types weren't made to be Primitives rather than Objects?
there's a certain power that comes from being able to use everything as objects. for a language with a very abstracted model where variables are purely being treated as references to objects, i dont know how primitives would fit that worldview that python offers.
Hmm
Like at the end of the day, python is excellent at what it's built for. cutting down developer time
Kinda make sense
huh
!pban @rotund quest nsfw
:incoming_envelope: :ok_hand: applied ban to @rotund quest permanently.
oh crap, I probably shouldn't've deleted
Why not
not having primitives means that the language is slower, but allows some things you couldn't otherwise have - like a unified type hierarchy, subclasses of builtin types, etc
I'd be able to report to discord
Banning from the server is enough
JavaScript has an illusion of having primitives which aren't called objects, but it makes matters worse 🙂
Python integers even have methods.
I mean, more like 🥲
I don't know much about js
i tried to search further for reasons why python chose to avoid primitives. one of the top article says "Python has four primitive types: integers, floats, booleans and strings. "
:/
!e ```py
x = 256
print(x.to_bytes(5, "little"))
@raven ridge :white_check_mark: Your eval job has completed with return code 0.
b'\x00\x01\x00\x00\x00'
Lmao
Yeah i know that int has methods
it's like those horrible python interview question lists
That's a bad article
not None? 😄
"Python has 5 datatypes" etc.
Just throw "everything is object" meme at these articles
haha yeah, unfortunate
"wait, so it is all objects? "
"always has been"
It seems impossible to come up with any definition for what "primitive" could possibly mean in Python that would include str but not bytes
even if you accept a "nontraditional" definition for "primitive", str and bytes are equally primitive.
I once wasted my time on reading an article about "how can python be faster than c++" and he compared multithreaded program to c++ single threaded program
for ints, i can atleast think of the "infinite precision" feature that's handy. would primitives even be able to support that?
A multithreaded Python program running CPU-bound tasks should be slower than a single-threaded one 🙂
I am not talking about "threading" module here though
I will try to found out the article
lets take an apple and compare it to an orange. 😛
I suppose there's no reason why you can't have a primitive bigint type
That basically what the article was about
primitive in the sense that it doesn't participate in the OOP object model
conversations here make me want to understand and peel away the layer of abstraction that im comfortable with, and actually try to understand the underlying semantics of it all
I've been writing some C++ for the last few days that has been driving me absolutely crazy for lack of expressiveness. I really, really just want to be able to return a type from some functions. 😕
@visual shadow oh yeah, they used Numba for python to make it faster
I could do some type erasure crap and come up with something that works, probably, but - not having types as first order objects has been killing me in this project.
What do you mean by "return a type from a function"
return int is how i understood it.
Me too
yeah, exactly.
Why would you do that?
perhaps what_dataype_i_need("params")("input") if i had to guess?
ie. the type is probably being used to process something down the line to create actual objects/values
or would have been*
because I have something whose type isn't known at compile time, but is known at runtime - which means I need to have functions at compile time that are equipped to deal with any possible type, and I need to decide which of them to call at runtime. But that means I need to do something like:
if (var.type == INT_TYPE)
return do_something_with_value<int>(var, param1, param2)
if (var.type == DOUBLE_TYPE)
return do_something_with_value<double>(var, param1, param2)
if (var.type == FLOAT_TYPE)
return do_something_with_value<float>(var, param1, param2)
🤢 that's terrible
it'd be much nicer to be able to do:
if (var.type == INT_TYPE)
T = int
else if (var.type == DOUBLE_TYPE)
T = double
else if (var.type == FLOAT_TYPE)
T = float
return do_something_with_value<T>(var, param1, param2)
but I can't, because variables can't hold types in C++.
@raven ridge i am sure there is an std function that check variable type
Like typeid()
that's probably covered in this part var.type == INT_TYPE.
nah, you're misunderstanding - I'm not checking what type a C++ object has, I'm checking what type of value it should be interpreted as.
Imagine the value is always a str, and I'm deciding whether to parse it as an int, or a double, or a float, based on a schema. Something like that.
ha, had a feeling it would be something like that
