a "hello world" python C extension (intermediate - advanced) anthony explains

แชร์
ฝัง
  • เผยแพร่เมื่อ 30 ม.ค. 2025

ความคิดเห็น • 36

  • @LoucuraMagic
    @LoucuraMagic 4 ปีที่แล้ว +5

    thank you, that was exactly what i was looking for!

  • @chriskorfmann
    @chriskorfmann 2 ปีที่แล้ว +3

    Awesome vid, as usual! There's not much out there on the C-API that is this straightforward.
    I recently wrote my first C-extension with a lot of frustration (I hated C before starting), but I now have an appreciation for it. I got a ~20% speed-up vs the same function implemented in vanilla Python. I'm curious to see how it stacks up against Cython, but I have no idea how to write .pyx

    • @imreolah6077
      @imreolah6077 2 หลายเดือนก่อน

      20% or 20x? later is more probably, c is the fastest, python is the slowest in runtime, If use numpy, or similar, you use c, just from python call.

  • @imreolah6077
    @imreolah6077 2 หลายเดือนก่อน +1

    What this enviroment/WM? I search for usable tiling solution...

    • @anthonywritescode
      @anthonywritescode  2 หลายเดือนก่อน

      it's my own! I call it the "anthony window manager"

  • @nicezombies1
    @nicezombies1 2 ปีที่แล้ว +1

    Does sys.getrefcount(ANY STRING) by default return 3?
    Because it didn't change after the code rewrite.

  • @dannymurphy2823
    @dannymurphy2823 4 ปีที่แล้ว +2

    Like watching your vids. Even thou I am still to grasping the core concepts of python language itself.

  • @fernandodiazurbano8815
    @fernandodiazurbano8815 2 ปีที่แล้ว

    This is a nice video, it was a good watch and to the point.

  • @TheNotStated
    @TheNotStated 3 ปีที่แล้ว +6

    Thanks for the video! Why we dont have to call Py_DECREF on name variable?

    • @anthonywritescode
      @anthonywritescode  3 ปีที่แล้ว +5

      PyArg_ParseTuple I believe gives borrowed references, so unless you're doing something with that object it doesn't need an increment or a decrement

  • @anibaldk
    @anibaldk 2 ปีที่แล้ว +1

    This is, most of the time, more of a hobby/curiosity than an actual need (which is still great so many thanks).
    However, it's usually best to check out libraries like numpy/scipy/numba/etc before writing your own C extension.

    • @anthonywritescode
      @anthonywritescode  2 ปีที่แล้ว

      this is terrible advice -- numpy does not help you interface with C libraries and is a behemoth of a dependency to pull in

    • @anibaldk
      @anibaldk 2 ปีที่แล้ว +1

      @@anthonywritescode Perhaps I did not explain myself correctly. I meant to say check it's best to check numpy (or other C based libraries) first than trying to implement the same code.
      I also don't quite follow the hassle of installing numpy. I never had any trouble with it 🤔
      Anyway, thanks for the video. I wanted to learn exactly what you demoed so I'm grateful for it 😊

    • @tens0r884
      @tens0r884 ปีที่แล้ว +1

      @@anibaldk I think you either dont quiet understand either what numba does or what the c api is for 😂😂

  • @POP3O2
    @POP3O2 3 ปีที่แล้ว +5

    im starting new job as software engineer and they are going to use capi for python 2.7. I find this video so useful. Thanks man! PS. What you like to continue with this topic? ;)

    • @anthonywritescode
      @anthonywritescode  3 ปีที่แล้ว +1

      whew 2.7 that's rough! glad this was helpful -- there's some other related things to check out if you don't want to write as much C: th-cam.com/video/X5irxO5VCHw/w-d-xo.html

  • @DataPastor
    @DataPastor ปีที่แล้ว

    Great video. A Zig version would also be nice.

  • @marcello4258
    @marcello4258 3 ปีที่แล้ว +5

    same for RUST and GO as mentioned please ;)

  • @victordvickie
    @victordvickie 3 ปีที่แล้ว +2

    Is there any advantage of using C++ over C for this and if so can you make a guide?

    • @anthonywritescode
      @anthonywritescode  3 ปีที่แล้ว +3

      the python bits have to be `extern "C"` at the least so integrating c++ is kind of tricky. there isn't really much upside other than c++ is a different language than C

    • @victordvickie
      @victordvickie 3 ปีที่แล้ว +1

      @@anthonywritescode thank you

    • @angelcaru
      @angelcaru 9 หลายเดือนก่อน +1

      "there isn't really much of an upside other than the downside"​@@anthonywritescode

  • @j2d51
    @j2d51 3 ปีที่แล้ว

    Why we use .c extension to save program

    • @anthonywritescode
      @anthonywritescode  3 ปีที่แล้ว +4

      the whole point of this video is to demo a C extension

  • @erjiaxiao6577
    @erjiaxiao6577 2 ปีที่แล้ว

    what does 'pip install .' do?

    • @anthonywritescode
      @anthonywritescode  2 ปีที่แล้ว +2

      it installs the source package at `.` (the current directory)

    • @erjiaxiao6577
      @erjiaxiao6577 2 ปีที่แล้ว

      @@anthonywritescode thanks a lot! it helps!

  • @mastan419
    @mastan419 4 ปีที่แล้ว +1

    Thanks sir

  • @Saimon404
    @Saimon404 3 ปีที่แล้ว

    How to decompile .so file?

    • @anthonywritescode
      @anthonywritescode  3 ปีที่แล้ว +2

      in the general case you cannot -- you can "disassemble" it (to assembly code) -- but getting it back to the original high-level language is impossible

    • @Saimon404
      @Saimon404 3 ปีที่แล้ว

      @@anthonywritescode ok bro,but....

  • @jccheloven
    @jccheloven 3 ปีที่แล้ว

    Too small font, can't see the code. A pity :-/

    • @anthonywritescode
      @anthonywritescode  3 ปีที่แล้ว +2

      the code samples are all up on github.com/anthonywritescode/explains
      I can't really go any larger on font size (it's already extremely large) -- I'd recommend *not* watching on a phone

    • @jccheloven
      @jccheloven 3 ปีที่แล้ว

      @@anthonywritescode Awesome, thank you!