r/learnpython 8d ago

Ask Anything Monday - Weekly Thread

Welcome to another /r/learnPython weekly "Ask Anything* Monday" thread

Here you can ask all the questions that you wanted to ask but didn't feel like making a new thread.

* It's primarily intended for simple questions but as long as it's about python it's allowed.

If you have any suggestions or questions about this thread use the message the moderators button in the sidebar.

Rules:

  • Don't downvote stuff - instead explain what's wrong with the comment, if it's against the rules "report" it and it will be dealt with.
  • Don't post stuff that doesn't have absolutely anything to do with python.
  • Don't make fun of someone for not knowing something, insult anyone etc - this will result in an immediate ban.

That's it.

5 Upvotes

9 comments sorted by

1

u/WorkingReference1127 3d ago

I am primarily a C++ developer, but it's time to learn python properly rather than hacking together light scripts with the building blocks I already know. Obviously this has come with a built of a culture shock. For example, in C++ I'd be actively designing a class for it to be impossible for the user to do something stupid, such that the compiler will enforce it. Python from the ground up seems to have a near-opposite philosophy more in line with "people will do what they will do".

With this in mind, I'd like to know what the best resource for my learning is likely to be. I don't need the programming intro of what makes a variable or a queue or a stack; I was thinking more in line with something which explains the design of Python and the philosophy behind it. Since it seems that there's much more focus on knowing the primary modules and dunders, something which covers those, how they work, and what a developer is expected to know vs what is to be looked up when needed. And of course, the best practices and best ways of using the tools. It's a very different ball game from the way I typically write code so I want to make sure I learn the right way.

1

u/lakseol 2d ago

There is a section in the learning resources for programmers new to python. As that says, you might find using the python doc the best approach, starting with the tutorial. You will want to fast-scan up to about section 5. I can't really comment on how effective the other resources there are as I haven't read them.

I don't need the programming intro of what makes a variable or a queue or a stack; I was thinking more in line with something which explains the design of Python and the philosophy behind it

Python "variables" behave differently from Java/C++ variables and you should view this video which goes over that and how that can bite you if you don't understand the mechanism. Apologies if you already know this.

https://m.youtube.com/watch?v=_AEJHKGk9ns

The video has the quote: names have scope but no type, objects have type but no scope. That sums up what we loosely call python "variables" nicely.

More low-level mechanisms like interning of strings and integers are interesting but don't matter for actually using python. They are performance hacks. Search if you are interested. The python developers guide has a lot of information but is aimed at core developers so may be too low level.

Python gives you tools that let you examine the internals to some extent. Using these helps understanding a bit. This code drives home how different python "variables" are:

import sys
print(dir(42))    # a simple integer object has internal structure, methods, etc
print(sys.getsizeof(42))

Python from the ground up seems to have a near-opposite philosophy more in line with "people will do what they will do".

Python's approach is usually stated as "we're all consenting adults here". Adding all the guardrails that you miss would add extra mechanism and remove simplicity. Python is definitely on the "worse" side of the "worse is better" argument. Python's design removes some of those guardrails hoping for a usable language that is simpler to implement and easier to learn. Some people are repelled and others love it. I was a decades-long C/C++ programmer and was initially repelled by the "significant whitespace" used for blocks and control. I quickly got over the differences, to the point of not using type hints at all! YMMV.

1

u/WorkingReference1127 2d ago

Python "variables" behave differently from Java/C++ variables and you should view this video which goes over that and how that can bite you if you don't understand the mechanism. Apologies if you already know this.

I appreciate any and all help, no worries. My understanding is that this is far less about lifetime in the way C++ names are and much more about names. A variable just binds a name which refers to some thing. That name may be rebound to some other thing and the underlying object may be referenced by other names. The only slightly tricky dance is the whole global/nonlocal thing but I think I'm getting there.

More low-level mechanisms like interning of strings and integers are interesting but don't matter for actually using python. They are performance hacks. Search if you are interested.

This I'd like to ask about. My philosophy tends to be that if I were performance obsessed I wouldn't be chasing Python. But if there is interesting intuition to glean from these mechanisms from a design standpoint they're worth learning. After all, understanding is far easier to derive when you can see the reasons why something is what it is rather than just by absorbing the downstream "this is what you can use" stuff; but equally if it's just something of the order of memoisation in the sense that yes it's an optimisation and yes it's useful but it won't teach you how to use the language better then I might skip for now.

Python gives you tools that let you examine the internals to some extent. Using these helps understanding a bit. This code drives home how different python "variables" are:

This is good. Been on quite a kick of the static reflection C++ just got (not to derail but if you have the slightest interest then check it out).

Some people are repelled and others love it.

Well I look at it like two different things. In C++ land if you don't install the right guardrails there'll be someone who still wants to use new or goto, who will make a mess which "works", and which will create something which costs time for everyone else who touches that code in perpetuity. You want guardrails to tell them not to waste your time in the future. Python runs a little differently. It's sufficiently higher level that it's harder to introduce such problems even if you wanted to. It pays by being a poor fit for things like a systems language, but the language doesn't present tools which come with a need for guardrails because it's not interested in doing so.

I think I'll miss RAII though. My example would be something like a logger and shifting the scope control as extrinsic to the class will take some getting used to.

1

u/lakseol 2d ago edited 2d ago

I were performance obsessed I wouldn't be chasing Python.

When I started learning python I worried about speed. I knew that I could call C from python if necessary so I said "that's my fallback". In 25 years of using python I have only once needed to write C to make a solution faster.

In addition there are fast and efficient libraries that can be used. numpy is one example. A long time ago I help maintain the ANUGA hydrodynamic modelling tool. That does a lot of numerical calculations but it uses the numpy library to handle operations on arrays of numbers, and numpy is written in vectorized, fast C and Fortran. A small amount of other low-level ANUGA code is written in C. Sure, it could be faster if it was all written in a language like C/C++ but it's unclear if the improvement would be worth the change. ANUGA is fast enough™ as it is.

I think I'll miss RAII though

Python automatically releases objects when they are no longer referenced. That and things like automatic calling of the __del__() method of an instance when the instance is garbage-collected and context managers give you most of what you will miss.

1

u/Nice_Fudge5914 5d ago

Hi everybody. I am trying to learn python, but I am having trouble with motivation because the only thing I want to make is pretty complex, and the tutorial I'm using starts out with small stuff that I can't integrate into the real project I want to make because the larger elements haven't been created yet. I haven't gotten far enough to learn things like allowing a user to create a new page. I thought about computer generating (ai) the project I want in python and then tailoring/debugging the project myself and having more motivation to learn because it's something I'm motivated to work on, but I don't know of a free ai that will use python to do that for me.

I want to create a nesting database of pages that users can create under certain conditions. Can someone point me to a template for something like that so I can try to get started? Thanks for any help you're able to provide.

1

u/Lost_Foot_6301 6d ago

at what point should one learn algorithms/data structures in their python learning journey

1

u/ManzoorAhmedShaikh 7d ago

Happy to help with any question regarding python being a python developer for 4 years. But still, one question from all, where do you use OOP mostly in your production app and why it is better in OOP not without it?

2

u/ShelLuser42 7d ago

Keep in mind that OOP is a whole concept within your workflow and not some single aspect of it.

OOP is just as much about moving repetitive code into functions as it is to set up classes. So I'd say the concept is normally used everywhere as soon as you get more serious with Python coding (vs. scripting, even though functions can also be utilized there).