Why doc call `__init__` as a method rather than function?

2023-09-15 Thread scruel tao via Python-list
```python
>>> class A:
...   def __init__(self):
... pass
...
>>> A.__init__

>>> a = A()
>>> a.__init__
>
```

On many books and even the official documents, it seems that many authors 
prefer to call `__init__` as a "method" rather than a "function".
The book PYTHON CRASH COURSE  mentioned that "A function that’s part of a class 
is a method.", however, ` A.__init__` tells that `__init__` is a function...

I wonder how can I call `__init__` as? Consider the output above.
Maybe both are OK? If you prefer or think that we must use one of the two, 
please explain the why, I really want to know, thanks!
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Why doc call `__init__` as a method rather than function?

2023-09-15 Thread Dan Sommers via Python-list
On 2023-09-15 at 10:49:10 +,
scruel tao via Python-list  wrote:

> ```python
> >>> class A:
> ...   def __init__(self):
> ... pass
> ...
> >>> A.__init__
> 
> >>> a = A()
> >>> a.__init__
> >
> ```
> 
> On many books and even the official documents, it seems that many authors 
> prefer to call `__init__` as a "method" rather than a "function".
> The book PYTHON CRASH COURSE  mentioned that "A function that’s part of a 
> class is a method.", however, ` A.__init__` tells that `__init__` is a 
> function...

I always call __init__ "the initializer."  YMMV.

> I wonder how can I call `__init__` as? Consider the output above.
> Maybe both are OK? If you prefer or think that we must use one of the two, 
> please explain the why, I really want to know, thanks!

Usually, you don't call (or even refer to) __init__ from your
application.  One __init__ can call another one in the case of
initializing superclasses.

When you evaluate A(), Python calls __init__ for you.  You can see this
if you add something "visible" to __init__, like a print statement.
-- 
https://mail.python.org/mailman/listinfo/python-list


PEP668 / pipx and "--editable" installs

2023-09-15 Thread c.buhtz--- via Python-list

Hello,
I wonder that today was the first day I stumbled over PEP668 / pipx and 
the "externally-managed-environment" problem. I migrated from Debian 11 
to 12.


I'm developing some Python packages; applications and libraries. I never 
used virtual environments and wouldn't like to start with it.
But I do use "--editable" for my packages. I wonder how I should go on 
with using "pipx" and "--editable" mode because I had some problems with 
it I couldn't fix following PEP668 but only with using 
"--break-system-packages". And PEP668 do not mention editable-mode.


The two example packages I use here are public available and clonable 
(is this a word?) via git.


Let's start with a command line application named "hyperorg" 
(https://codeberg.org/buhtz/hyperorg).
I tried to install it via "pipx install -e .[develop]". It's 
pyproject.toml has a bug: A missing dependency "dateutil". But 
"dateutil" is not available from PyPi for Python 3.11 (the default in 
Debian 12). But thanks to great Debian they have a "python3-dateutil" 
package. I installed it.
But "hyperorg" do ignore it and can't import it. Of course I 
re-installed hyperorg via pipx.
Installing it via "pip" and "--break-system-packages" it works well and 
do see the "python3-dateutil" package.


The second example is a "library" named "buhtzology" 
(https://codeberg.org/buhtz/buhtzology).
Here it seems that "pipx" do refuse to install it in the first place 
("pipx install -e .[develop]") and gave me this message.


"No apps associated with package buhtzology. Try again with 
'--include-deps' to include apps of dependent packages, which are listed 
above. If you are attempting to install a library, pipx should not be 
used. Consider using pip or a similar tool instead."


What is the difference between an "app" (it is a google/apple marketing 
term used for software with user interaction. Just name it application.) 
and a "library" in context of pip(x)? Why does it count?
I tried again with "pipx install --include-deps -e .[develop]" and got 
an output I'm not sure if this is an error or a success message. I 
assume it is an error because I'm not able to "import buhtzology". It 
seems like that the package was not installed.


⚠️  Note: normalizer was already on your PATH at /usr/bin/normalizer
⚠️  Note: f2py was already on your PATH at /usr/bin/f2py
⚠️  Note: f2py3 was already on your PATH at /usr/bin/f2py3
⚠️  Note: f2py3.11 was already on your PATH at /usr/bin/f2py3.11
⚠️  Note: py.test was already on your PATH at /usr/bin/py.test
⚠️  Note: pytest was already on your PATH at /usr/bin/pytest
⚠️  File exists at /home/user/.local/bin/pycodestyle and points to 
/home/user/.local/bin/pycodestyle, not 
/home/user/.local/pipx/venvs/buhtzology/bin/pycodestyle. Not modifying.
⚠️  File exists at /home/user/.local/bin/ruff and points to 
/home/user/.local/bin/ruff, not 
/home/user/.local/pipx/venvs/buhtzology/bin/ruff. Not modifying.

  installed package buhtzology 0.2.0.dev0, installed using Python 3.11.2
  These apps are now globally available
- f2py
- f2py3
- f2py3.11
- fonttools
- normalizer
- py.test
- pyftmerge
- pyftsubset
- pytest
- tabulate
- ttx
- pycodestyle (symlink missing or pointing to unexpected location)
- ruff (symlink missing or pointing to unexpected location)
done! ✨ 🌟 ✨

BUT!

$ python3 -c "import buhtzology"
Traceback (most recent call last):
  File "", line 1, in 
ModuleNotFoundError: No module named 'buhtzology'
--
https://mail.python.org/mailman/listinfo/python-list


Re: Why doc call `__init__` as a method rather than function?

2023-09-15 Thread Clara Spealman via Python-list
All methods are functions, but not all functions are methods. All methods
are functions in the namespace of a class and when resolved from the class
directly, you'll get the original function. Its only when resolved from an
*instance* of the class, as in self.__init__ or self.any_other_method(),
that the function is wrapped in a method object, which is callable and
binds the function to the particular instance passed as "self".

So the simple answer is "its both". The only slightly less simple answer is
"its both, depending on how you're getting it."

On Fri, Sep 15, 2023 at 6:53 AM scruel tao via Python-list <
[email protected]> wrote:

> ```python
> >>> class A:
> ...   def __init__(self):
> ... pass
> ...
> >>> A.__init__
> 
> >>> a = A()
> >>> a.__init__
> >
> ```
>
> On many books and even the official documents, it seems that many authors
> prefer to call `__init__` as a "method" rather than a "function".
> The book PYTHON CRASH COURSE  mentioned that "A function that’s part of a
> class is a method.", however, ` A.__init__` tells that `__init__` is a
> function...
>
> I wonder how can I call `__init__` as? Consider the output above.
> Maybe both are OK? If you prefer or think that we must use one of the two,
> please explain the why, I really want to know, thanks!
> --
> https://mail.python.org/mailman/listinfo/python-list
>
>

-- 

CALVIN SPEALMAN

SENIOR QUALITY ENGINEER

[email protected]  M: +1.336.210.5107
[image: https://red.ht/sig] 
TRIED. TESTED. TRUSTED. 
-- 
https://mail.python.org/mailman/listinfo/python-list


Postgresql equivalent of Python's timeit?

2023-09-15 Thread Albert-Jan Roskam via Python-list
   Hi,
   This is more related to Postgresql than to Python, I hope this is ok.
   I want to measure Postgres queries N times, much like Python timeit
   (https://docs.python.org/3/library/timeit.html). I know about EXPLAIN
   ANALYZE and psql \timing, but there's quite a bit of variation in the
   times. Is there a timeit-like function in Postgresql?
   Thanks!
   Albert-Jan
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Postgresql equivalent of Python's timeit?

2023-09-15 Thread Peter J. Holzer via Python-list
On 2023-09-15 17:42:06 +0200, Albert-Jan Roskam via Python-list wrote:
>This is more related to Postgresql than to Python, I hope this is ok.
>I want to measure Postgres queries N times, much like Python timeit
>(https://docs.python.org/3/library/timeit.html). I know about EXPLAIN
>ANALYZE and psql \timing, but there's quite a bit of variation in the
>times. Is there a timeit-like function in Postgresql?

Why not simply call it n times from Python?

(But be aware that calling the same query n times in a row is likely to be
unrealistically fast because most of the data will already be in
memory.)

hp

-- 
   _  | Peter J. Holzer| Story must make more sense than reality.
|_|_) ||
| |   | [email protected] |-- Charles Stross, "Creative writing
__/   | http://www.hjp.at/ |   challenge!"


signature.asc
Description: PGP signature
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Why doc call `__init__` as a method rather than function?

2023-09-15 Thread Alan Gauld via Python-list
On 15/09/2023 11:49, scruel tao via Python-list wrote:
> ```python
 class A:
> ...   def __init__(self):
> ... pass

> On many books and even the official documents, it seems that 
> many authors prefer to call `__init__` as a "method" rather 
> than a "function".

That' because in OOP terminology methods are traditionally
implemented as functions defined inside a class (There are
other ways to define methods in other languages, but the
class/function duology is by far the most common.)

What is a method? It is the way that an object handles a
message. OOP is all about objects sending messages to each
other. Many different objects can receive the same message
but they each have their own method of handling that message.
(This is called polymorphism.)

Over time the most common way to implememt OOP in a language
is to invent a "class" structure and to allow functions to
either be defined within it or added to it later. In either
case these functions are what define how a class (and its
object instances) handle a given message. So the function
describes the method for that message. And over time that
has become shortened to the function inside a class *is*
its method.

In practice, the message looks like a function call. And
the method consists of the function body (and/or any
inherited function body). Thus every method is a function
(or set of functions) but not every function is a  method
(or part of one).

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


-- 
https://mail.python.org/mailman/listinfo/python-list


`time.perf_counter_ns` always a 64-bit int?

2023-09-15 Thread rmlibre--- via Python-list
I'd like to capture the output of `time.perf_counter_ns()` as an 8-byte
timestamp.

I'm aware that the docs provide an undefined start value for that clock.
I'm going to assume that means it can't be expected to fit within 8
bytes. However, it would be rather convenient if it could. Does anyone
know if any such platform agnostic assumption could be made for any
fixed number of bytes, even if it isn't exactly 8 bytes?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: PEP668 / pipx and "--editable" installs

2023-09-15 Thread Rimu Atkinson via Python-list





I never 
used virtual environments and wouldn't like to start with it.


There's your problem - everything else is a result of this. There is 
just no nice way to work with a combination of pypi, apt-get and 
system-wide python.


Everyone uses virtual environments.

Sorry.
--
https://mail.python.org/mailman/listinfo/python-list


Re: PEP668 / pipx and "--editable" installs

2023-09-15 Thread c.buhtz--- via Python-list
Dear Rimu,

thanks for your reply. I'm willing to learn for sure.

On 2023-09-16 14:17 Rimu Atkinson via Python-list
 wrote:
> There's your problem - everything else is a result of this. There is 
> just no nice way to work with a combination of pypi, apt-get and 
> system-wide python.
> 
> Everyone uses virtual environments.

It is nothing bad about using virtual environments but also not about
not using them. In my own work I haven't see a use case where I needed
them. And I expect that some day I'll encounter a use case for it. This
here is not about pro and cons of virtual environments.

Please explain how the two problems I explained are influenced by not
using virtual environments.

As I explained I tried to use "pipx" (the X at the end!) because it
seems to be the recommended way today because of PEP668. And to my
knowledge pipx do use a virtual environment in the back. Nice.

But it did not work the way I expected.

I don't say that pipx is wrong or buggy. I assume I misunderstood
something in context of PEP668 and in the use of pipx. That is why I'm
asking here.
-- 
https://mail.python.org/mailman/listinfo/python-list