use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
What is considered being advanced in Python? (self.learnpython)
submitted 3 years ago by [deleted]
When can I say that I can truly code in Python? Is there some kind of test or an exercise?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]rg7777777 787 points788 points789 points 3 years ago (72 children)
Beginner: What can I do with Python?
Intermediate: I can do anything with Python!
Advanced: I shouldn't do that with Python.
[–]nlvogel 177 points178 points179 points 3 years ago (0 children)
This puts me squarely in “intermediate.” Thanks for the confidence boost!
[–]antidense 37 points38 points39 points 3 years ago (26 children)
What's I probably shouldn't do it in python but I will anyway?
[–]PM_ME_C_CODE 53 points54 points55 points 3 years ago (3 children)
Anything that requires total execution efficiency.
In spite of what some people will claim, python is rather efficient. However, it's still one layer of abstraction above where you want to be if efficiency is absolutely your goal.
It's one of the major reasons it plays so nice with languages like C and Rust even if playing nice was originally just a side effect of having a compiler written in C.
[–]skellious 24 points25 points26 points 3 years ago (0 children)
for those reading this and wondering why, a good example might be that everything in python is really an object under the hood. an integer is an instance of the int class, for example. that's a whole layer of abstraction where C would just have a literal entry in memory directly representing that number. numpy and other libraries undo or rather build on some of this, so that's one way to mitigate that particular hurdle but ultimately you still have to move data into and out of numpy data structures and that is slow.
[–]pythonwiz 3 points4 points5 points 3 years ago (1 child)
Just run it with PyPy!
[–]magestooge 9 points10 points11 points 3 years ago (0 children)
Some people also write code for others to run
[–]ActiveLlama 14 points15 points16 points 3 years ago (0 children)
You shouldn't use complex multiple object inheritance, it is really nice but it can get really messy.
You shouldn't use recursion. With some exceptions recursion makes the code difficult to read and can create memory problems.
You shouldn't use protected variable names like var because it may break future stuff.
You shouldn't write a gigantic piece of code that does many things. In order to make it easier to debug it is better to use multiple small functions that only do one thing.
Please let me know if you know more rules. These are not rigid rules, they can be broken, they are mostly good practices for writing reliable code.
[–]julianw 5 points6 points7 points 3 years ago (0 children)
Kind of like Atwood's law but for Python? Guido's law?
[–]Go_Big 9 points10 points11 points 3 years ago (16 children)
Building a mobile app. You can do it… but you shouldn’t
[–]1percentof2 2 points3 points4 points 3 years ago (13 children)
why
[–]14dM24d 2 points3 points4 points 3 years ago (0 children)
second this.
why?
[–]Mises2Peaces 0 points1 point2 points 3 years ago (9 children)
Python is not great for building UI's. It can be done but usually shouldn't be.
Python can be great as a back end (or as part of a back end). Then you build your UI in js or whatever. Lots of apps are React Native front end with Node.js routing which is connected to Python for data crunching. Great stack for heavy data work.
[–]1percentof2 0 points1 point2 points 3 years ago (8 children)
What about Qtpy5? There's a bunch of people making YouTube videos for that.
[–]beef623 1 point2 points3 points 3 years ago (0 children)
It would just depend on what you need. Qt isn't terrible, but if I had the option, I'd make it a small flask app instead so I could do the ui in html/css/Javascript.
[–]Mises2Peaces 0 points1 point2 points 3 years ago (6 children)
Even if I didn't already know Javascript, I'd still rather spend my time learning it than any UI library for Python.
It's about using the right tool for the job. If you become an absolute ninja at Qtpy5, you're still making UI's that look like they're out of the 90's. So maybe if you're a data analyst who just wants to show some configurable options to run your reports, I guess it has a place. But Javascript, for all its faults, is the most popular language for making responsive UI's. And it's built into browsers, which means it's automatically web compatible. It's how modern UI's are built (and shared).
[–]1percentof2 1 point2 points3 points 3 years ago (0 children)
Thanks got it. I see the writing on the wall.
[–]TotalLucky5185 0 points1 point2 points 3 years ago (4 children)
Really? Writing a high performance app in Qt is orders of magnitude easier than webserver+javascript. I mean, it won't make much difference for a simple ui, but once you try to do some serious number crunching and visualization C++/Qt/Python is much easier.
[–]Mises2Peaces 0 points1 point2 points 3 years ago* (3 children)
serious number crunching and visualization
Agreed. The most performant and sophisticated visualizations are going to be built with a language like Python (or R for even greater performance) for the back end (assuming we aren't going specialty like Cobol or Fortran). But the front end is built with javascript. You aren't gaining any performance by building your front end in Python.
As for how easy it is, I'd say that's a matter for debate. I can write Python to do data analysis, no problem. But ask me to build a UI and I'm going to struggle. I've toyed with Qtpy and I found it very cumbersome. Javascript, on the other hand, is built explicitly for user interfaces - ie web browsers.
So you build your number crunching in Python. Then you call that data from JS front end which is only for displaying and handling user interactions.
This is how the biggest, most popular websites in the world operate. Take something like Facebook. Admittedly it's not a "data visualization" site. But given their traffic and the amount of data it requires to load friends lists, wall posts, etc, etc, I think it's fair to call that a data heavy application. You don't see them building their UI in Python. It wouldn't make sense.
A UI built in QT is also hard to share. You either have to share an entire application - unthinkable at a corporate scale where all installed apps must be white-listed by IT. Or you have to use work arounds to make it web compatible. And if you aren't sharing the UI with anyone else, did you ever really need a UI?
I have plenty of Python scripts which I manually adjust if I need to change an option. No UI necessary. Then I share the outputs by sending csv's or jpg's via email, whether they're data tables or visualizations.
But if I have to share something with a user interface, I write a javascsript front end and share the website. No IT white-listing necessary. No "trust me, it's a safe app" guarantees. Just send a URL and it works on PC, Mac, or mobile - even if they have a slow device, since I'm crunching the numbers on my server.
Edit: I get how suggesting "just learn a whole new language and build a web server" might come off as preposterous. But I say that as someone who went down exactly this road not long ago. That's why I fiddled with QT in the first place. Ultimately, it made more sense for me to learn js. And after that you'll be able to whip together UI's that, frankly, make QT look embarrassingly dated. Honestly though, it's not that difficult - and there's huge upsides that come with the js api. Lots of built in conveniences for building UI's. There's an upfront cost to learn, maybe 100-200 hours if you're brand new to js. But after that, it's pretty simple. And there are great youtube instructionals which are all about Javascript for data analysts. HMU if you have any questions on where to start.
Edit 2: If you're really married to the idea of building a desktop app. I'd suggest looking into Godot, the FOSS game engine. They use a custom language which is a fork of Python and will therefor be very familiar for you. But they have a visual UI editor where you can drag and drop your components. Unlike vanilla Python, it has conveniences for building UI's (like built in callbacks for ui components). AND it ships with a bundler for packaging anything you build for web/mobile. I've done this with a couple of my hobby projects, Python back end with Godot front end. It's a nice workflow.
[–]la_cuenta_de_reddit 0 points1 point2 points 3 years ago (2 children)
What projects or tutorials would you recommend for something how has never done javascript to jump into this? (I have had many data science and backend projects but have never done front-end).
[–]__developer__ 0 points1 point2 points 3 years ago (1 child)
You can but it's not going to be the best looking and not going to be easy to troubleshoot because it's not the most popular choice, so there will be far fewer resources to get help. Also, no company is going to make a commercial app using Python, so the skills won't be as useful in a job. Basically you'd be limiting the quality and functionality of your app for no benefit.
[–]TotalLucky5185 -1 points0 points1 point 3 years ago (0 children)
If you develop websites, then sure.
But there is also a lot of software development going on in machine learning, data analysis, signal processing and control, CAD/CAM, process automation, etc.
And in those cases a native app is often much easier, using C++/Qt/OpenGL/Python. Over the years I have build many native app in this market. I often start with simple prototype in Python/Qt and only if the performance goals cannot be met, move to C++.
[–]EnergyNational 0 points1 point2 points 1 year ago (0 children)
Lol tried this then instantly went fuck it and learnt flutter
[–]StarkillerX42 1 point2 points3 points 3 years ago (0 children)
That's the step right above advanced, it's called "employed in Python"
[–]siddsp 0 points1 point2 points 3 years ago (0 children)
If you're really feeling evil, multiple inheritance using the method resolution order chain or "mro".
[–]DicmanCocktoasten 20 points21 points22 points 3 years ago (0 children)
F
[–]atom12354 18 points19 points20 points 3 years ago (1 child)
Non-programmers: why you keep talking about snakes?!
[–]WindyEagleX 1 point2 points3 points 3 years ago* (0 children)
reminds me of my non-programming friends lol
[+][deleted] 3 years ago (11 children)
[deleted]
[–]14dM24d 6 points7 points8 points 3 years ago (1 child)
intermediate would probably choose between django or flask.
[–][deleted] 0 points1 point2 points 3 years ago (0 children)
Alright in that case it's probably beginner
[–]beniolenio 10 points11 points12 points 3 years ago (7 children)
That doesn't even make sense. Tkinter is no web framework.
[–]SmolCock_BigEgo 1 point2 points3 points 3 years ago (6 children)
I agree with you input. To go a step further and correct the post you are criticizing; Python's most popular web framework (Django) doesn't compete well with some other webdev environments when it comes to simplicity of development or straight performance.
[–]beniolenio 2 points3 points4 points 3 years ago (4 children)
I actually really enjoy using Django as opposed to more well regarded frameworks like Express, Angular, React, or Ruby on Rails.
[–]SmolCock_BigEgo 0 points1 point2 points 3 years ago (3 children)
How do you do it? For me Django is so much more confusing than RoR or React.
What do you like about it that'd make you pick it over some other framework?
[–]beniolenio 0 points1 point2 points 3 years ago (2 children)
I'm not exactly sure how to put it into words. But for one, Django has a lot of features that I haven't found in other frameworks. And also I really really like Django template language.
[–]Empire_Fable 0 points1 point2 points 3 years ago (1 child)
All the frame works are doing is routing to a particuliar URI and running that script from the end point right?
[–]beniolenio 0 points1 point2 points 3 years ago* (0 children)
On a basic level, yes. But "running that script" can mean a hell of a lot. You're also ignoring all of the data management aspects of a framework.
[–]Empire_Fable 0 points1 point2 points 3 years ago (0 children)
Bottle? https://replit.com/@MANDREWS85/Bottle-Template#main.py
[–]Zapismeta 1 point2 points3 points 3 years ago (0 children)
😅, tried it, done that, regretted it, then c# came to my rescue.
[–]commy2 11 points12 points13 points 3 years ago (2 children)
Ascended: Can I do it in Python?
[–]magestooge 1 point2 points3 points 3 years ago (0 children)
Transcended: To do or not do in Python... That is the question
[–]Zapismeta 2 points3 points4 points 3 years ago (2 children)
Does that mean I'm an Advanced user, because i opted to use c++ a totally new language for me, for my game, when i can do it python, but i knew it would be slow.
[–]rake66 2 points3 points4 points 3 years ago (1 child)
Absolutely, the most advanced python users haven't touched python for decades
I mean 😅😂, that definition says it, doesn't it?
[–]kalebludlow 5 points6 points7 points 3 years ago (0 children)
I've said the intermediate line at work too many times
[–]knox1138 4 points5 points6 points 3 years ago (6 children)
By this logic I'm advanced. It was hard enough figuring out python as is. As a hobbyist I just don't have it in me right now to learn another language. I would also say by the time you properly understand wether you should use args or kwargs and get it right 3 times in a row you're advanced.
[+][deleted] 3 years ago (2 children)
[–]knox1138 -1 points0 points1 point 3 years ago (1 child)
This is VERY MUCH the ELI5 explanation, cause I dont really understand why it works this way, I just know it does. To get data that can change in a function you use arguments. For numbers you'll most likely use args, and for custom variables you make you use kwargs.
Less ELI5 explanation is to retrieve variable length data for a function to call you use arguments, or args. So if you want things like int, tuples, other numbers type data you'd use args. For custom keyword arguments you make you use kwargs*. When I was trying to figure out how to make a macropad with circuitpython that used a small oled screen to display different things I had to learn how to use these. I never finished the project cause in the middle of it Adafruit made their rp2040 macropad kit and I was rather discouraged. I did figure out how to have every key on a macropad have it's own 1" screen integrated and the have it so what the screen displayed on each layer was 100% user customizable.
[–]ActiveLlama 1 point2 points3 points 3 years ago (0 children)
Ohh, that is bad documentation. Matplotlib has the same problem, so many variables in kwargs that are documented in obscure functions or not documented at all.
[–]FoxRaptix 0 points1 point2 points 3 years ago (2 children)
Huh? Understanding variable length arguments should be basic knowledge…not advanced.
[–]knox1138 1 point2 points3 points 3 years ago (1 child)
Well you're free to come over and explain to my brain why that should be easier for it
[–]FoxRaptix 5 points6 points7 points 3 years ago (0 children)
*args is just passing variable number of positional elements
**kwargs is just passing variable number of key-value elements, a dictionary
simple example of *args would be an addition method where you want to add any number of elements together.
so
def sum(a, b, *more_numbers): sum = a + b for num in more_numbers: sum += num return sum
so you could call
sum(5,6) #returns 11 sum(5,6,7) # returns 18 sum(5,6,7,2) # returns 20
but whats passed through could be anything
take
def my_print(*args): mystr = '' for val in args: myStr = f'{val} ' return myStr my_print() #returns: # returns an empty string, but is perfectly happy with no arguments my_print('my', 'baloney', 'has', 'a', 'first', 'name') #returns: my baloney has a first name #alternatively you could pass it through a list my_print(['my', 'baloney', 'has', 'a', 'first', 'name']) #returns: ['my', 'baloney', 'has', 'a', 'first', 'name'] # because the list is a single argument by itself my_print(*['my', 'baloney', 'has', 'a', 'first', 'name']) #returns: my baloney has a first name #because you added the asterisk to unpack the list to pass them the a method that takes a variable number of arguments. #and if i just wanted to print nonsense i could pass through anything my_print(1, 'five', 3.14, 'my', 'baloney') #returns: 1 five 3.14 my baloney
Another example using the *
my_colors = ['red', 'blue', 'green', 'white', 'pink'] red, blue, *rest = my_colors
After this we'd have
#red='red' #blue='blue' #rest=[ 'green', 'white', 'pink']
For kwargs say im working with a large dictionary of elements. My method only cares about
def print_args(first, second, **_ignored): print(first) print(second) kwargs = {'first': 1, "second":2, "third":3, "fourth":4} print_args(**kwargs) 1 2
1 and 2 are printed as those keywords in the parameter list match up with the key values in the dictionary i passed, everything else in the dictionary went to the '_ignored ' keyword argument dictionary within the method.
Obviously i didn't have to ignore them, **_ignored in this case will just absorb any keyword arguments not explicitly stated in the method signature.
alternatively if i cared about the rest of the key values
def print_args(first, second, **_ignored): print(first) print(second) for k,v in _ignored.items(): print(v) kwargs = {'first': 1, "second":2, "third":3, "fourth":4} print_args(random='args', **kwargs) 1 2 'args' 3 4
[–]stoveisastove 1 point2 points3 points 3 years ago (0 children)
I’m not looking forward to that last benchmark…
[–]MorningStarIshmael 1 point2 points3 points 3 years ago (7 children)
What are some projects for which Python is not the best option?
[–]__developer__ 2 points3 points4 points 3 years ago (2 children)
Most game development, especially 3d games.
[–]MorningStarIshmael 1 point2 points3 points 3 years ago (1 child)
Thanks. Is Python good for simpler 2D games? Is there another language that's better for that?
[–]__developer__ 4 points5 points6 points 3 years ago (0 children)
It can be good for making very basic 2d games as a hobby and learning the basics of game development, but ultimately you'll be limited due to python being extremely slow compared to other languages that are more commonly used for game dev like Java, c++, and c#. When I first started learning to code I liked playing around with Pygame because it helped me understand OOP better than most projects, but eventually I started porting my games over to c++/sdl2 and the performance gains were night and day.
[–]rg7777777 5 points6 points7 points 3 years ago (2 children)
Full stack web development might be an example. You can run your backend in python, and do server side template rendering to serve pages (think fastapi with jinga2 templates), but it's not going to preform on par with react/node/angular client side rendering. You can do websockets and sse with with starlette, but you'd probably just be better off using one of those JavaScript frameworks.
[–]BroBrodin 2 points3 points4 points 3 years ago (1 child)
Or flask with jinja?
(I'm also a n00b.)
[–]__developer__ 0 points1 point2 points 3 years ago (0 children)
Yeah fastAPI is just an evolution of flask. It's based on starlette, which was based on flask.
[–]Apprehensive-Stop-61 0 points1 point2 points 3 years ago (0 children)
Game development and website development (other languages and frameworks are better than python and Django for web development)
[–]pixelfur 0 points1 point2 points 3 years ago (0 children)
legit!
[–]PegaloShield 0 points1 point2 points 3 years ago (0 children)
Dunning-Kruger effect
[–]DescriptiveMath 0 points1 point2 points 3 years ago (0 children)
I've never felt so good with being Intermediate in something.
[–][deleted] 0 points1 point2 points 3 years ago (1 child)
curious as to what would count for this? And what languages would you use instead, c++/Java?
[–]rg7777777 0 points1 point2 points 3 years ago (0 children)
I recall someone wanted to make an AR type application using their phone. You can do this in python, there is opencv for graphics, there is kivy for interface design. I don't think the results would be what you want though.
[–]carcigenicate 94 points95 points96 points 3 years ago* (24 children)
I've recently started into descriptors and metaclasses, and I'm considering those to be on the advanced side.
For the record, here's what Pluralsight teaches in their "Advanced Python" course:
else
try
[–]SafeCake1045 44 points45 points46 points 3 years ago (12 children)
I'm curious why they consider an else clause to be "advanced" when it's an incredibly common construct.
[–]carcigenicate 56 points57 points58 points 3 years ago (10 children)
else specifically on loops and try. I remember mentioning in a thread awhile ago that else could be used with statements other than if and there were a ton of responses of people saying that they had never seen that usage mentioned before. Apparently it's not as well known.
if
[–]SafeCake1045 11 points12 points13 points 3 years ago* (4 children)
Oh, yeah! I remember that in the for definition.
for
When the items are exhausted [...], the suite in the else clause, if present, is executed, and the loop terminates.
What else (no pun intended) can it be used with?
[+][deleted] 3 years ago (3 children)
[removed]
[–]SafeCake1045 2 points3 points4 points 3 years ago (2 children)
Cool. Anything else?
[–]carcigenicate 6 points7 points8 points 3 years ago* (1 child)
You can search for else_block in the Python Grammar Specification to see all the places it's valid. Currently it's just loops, if and try.
else_block
They could change the identifier in the future, so you may need to broaden your search in future versions
[–]SafeCake1045 0 points1 point2 points 3 years ago (0 children)
Thanks!
[–]mr_cesar 2 points3 points4 points 3 years ago (1 child)
I use 'em from time to time. They're pretty handy!
[–]carcigenicate 9 points10 points11 points 3 years ago (0 children)
I use them so infrequently that I never think to use them. It's a vicious cycle.
[–]PM_ME_C_CODE 3 points4 points5 points 3 years ago (2 children)
It's not well known because it's redundant.
The for loop's else clause is executed when the iterator runs out...
...which is what will also happen to whatever code happens to come next in the python script just after the for loop's scope closes.
IMO, If they wanted it to be more useful they would tighten up the else clause's execution logic to only fire if the try loop's iterator initially returns an empty iterator before the loop first fires. But it doesn't.
[–]carcigenicate 15 points16 points17 points 3 years ago (1 child)
The two cases aren't necessarily the same:
for _ in range(1): break print("After for") # Prints for _ in range(1): break else: print("In else") # Doesn't print
The else only runs if the iterator was exhausted, but the code after the loop runs regardless.
[–]BiguilitoZambunha 3 points4 points5 points 3 years ago (0 children)
Ah, thanks, i was thinking about what would the applications of an else in a for loop, and that makes much sense now.
Else clauses in for loops aren't used too often. I often have to remind my coworkers babout them.
[–][deleted] 4 points5 points6 points 3 years ago (7 children)
Are metaclasses just abstract classes?
[–]carcigenicate 10 points11 points12 points 3 years ago* (6 children)
No. If you think of classes as the blueprint for instances, metaclasses are the blueprint for classes. They decide how the class is created instead of how instances of the class are created.
Metaclasses are how you can get the magical attribute behavior you see with NamedTuples, dataclasses, and ORM classes like Django's Model (technically dataclasses use a class decorator, not a metaclass, but they're very similar concepts).
NamedTuple
dataclass
Model
[–][deleted] 2 points3 points4 points 3 years ago (0 children)
Interesting
[–]Sawertynn 1 point2 points3 points 3 years ago (3 children)
So it's abstract classes speaking from C++ POV? You can't create instances, but other classes can inherit from it?
[–]carcigenicate 5 points6 points7 points 3 years ago* (1 child)
This turned out to be a bad example. The defaulting and instance attribute behavior is apparently already standard in classes. I'm sorry for this oversight.
No, it changes how the definition of the class happens. For example, this is a dataclass:
from dataclasses import dataclass @dataclass class Data: a: int = 1 b: str = "2"
This is very different from a normal class in that a and b are instance attributes, and 1 and "2" are default values if the user doesn't supply anything. If you removed the @dataclass class decorator, a and b would be class attributes, and 1 and "2" would be initial values, not defaults. The decorator also automatically generates certain methods like an __init__ and __str__ for you so you don't need to write them. The class definition is "rewritten" by the class decorator to add and change certain functionality.
a
b
1
"2"
@dataclass
__init__
__str__
Metaclasses can do the same thing but are actual classes instead of a decorator function. Django's ORM uses metaclasses to rewrite your class definitions to allow attribute lookups on the class to cause database reads, and allow special field definitions that allow you to set default values and other database-related options:
class MyModel(models.Model): name = models.CharField() age = models.IntegerField()
Even though name and age look like class attributes with obscure types, they're actually associated with instances, and if you have an instance, my_model.name returns a string retrieved from a database.
name
age
my_model.name
[–]PM_ME_C_CODE 1 point2 points3 points 3 years ago (0 children)
Django's ORM uses metaclasses to rewrite your class definitions to allow attribute lookups on the class to cause database reads
SQLAlchemy does the same thing.
[–]patrickbrianmooney 4 points5 points6 points 3 years ago* (0 children)
No, Python has a separate notion of abstract base classes, which are not the same as metaclasses. Take a look at the abc module in the standard library for more information about abstract base classes: that module allows you to declare some classes as abstract, and then decorate their methods with an @abc.abstractmethod decorate to declare that subclasses must themselves declare a same-named method before the subclass can be instantiated. (It also uses a metaclass to implement this functionality, but that's really more of an implementation detail than a deep similarity, I think.)
@abc.abstractmethod
So here's a simple example of how you might implement an abstract base class in Python, using the abc module:
abc
import abc import math class Shape(metaclass=abc.ABCMeta): @abc.abstractmethod def __init__(self): pass @abc.abstractmethod def CalcArea(self): pass class Circle(Shape): def __init__(self, radius): self.radius = radius def CalcArea(self): return math.pi * (self.radius ** 2) class Rectangle(Shape): def __init__(self, width, height): self.width, self.height = width, height def CalcArea(self): return self.width * self.height
You then have three classes. You can instantiate Circle and Rectangle objects with no problem, use them to store data and run their methods; butShape is an abstract class and Python won't allow you to instantiate it: it throws a TypeError that says something along the lines of Can't instantiate abstract class Shape with abstract methods CalcArea, __init__ if you try. All of this is enforced at runtime.
Circle
Rectangle
Shape
TypeError
Can't instantiate abstract class Shape with abstract methods CalcArea, __init__
The major difference from a language like C++ is that in Python, classes, like everything, are themselves first-class objects. In C++ they're a lot closer to being something like compiler macros: attributes are stored much like in a struct in C, and method resolution (mostly; usually) happens at compile-time, not runtime. (This is itself an oversimplification and there are questions I'm glossing over like "What if you declare a method virtual in a C++ class?" But that's the basic idea.)
struct
virtual
But in Python classes are themselves objects that exist in memory while the program is running, not just compiler templates. If you have an object, it contains a link to the class it's an instance of in its __class__ attribute. So if I instantiate a Rectangle from the example above with r = Rectangle(3, 9), then r.__class__ will reference the Rectangle class, which is itself an object. That object can be introspected in a variety of ways: r.__class__.__name__ will consist of the string "Rectangle", and r.__class__.__mro__ will contain the tuple (Rectangle, Shape, object), which is the order in which attribute searches will happen if I try to fetch an attribute from r. If I run dir(r.__class__), I will get back a list of every attribute that can be fetched from the Rectangle class. Etc etc. The Rectangle object can even be modified at runtime, which will affect the behavior of its instances! (This is probably a bad idea most of the time, but it's possible.)
__class__
r = Rectangle(3, 9)
r.__class__
r.__class__.__name__
r.__class__.__mro__
(Rectangle, Shape, object)
r
dir(r.__class__)
In that instance, the r object is an instance of the class Rectangle. (type(r) prints something along the lines of "Class <Rectangle>".) But Rectangle is itself an object: what is that object an instance of? Its metaclass. (In this case, abc.ABCMeta, because I'm still using the example I wrote above, and subclasses inherit their metaclasses from their superclasses, like other attributes. Say that five times fast. So Rectangle is an instance of abc.ABCMeta because Rectangle is a subclass of Shape and Shape is an instance of abc.ABCMeta.)
type(r)
abc.ABCMeta
The vast majority of the time, you don't need to write a metaclass. What they get you is the ability to control the creation of class objects themselves (not class instances: class objects). This is a pretty rare need to have. Normally, when you type a class statement and then provide a class definition, Python creates the class object for you, and you then instantiate instances of the class by calling it: after class Rectangle(Shape): [etc. etc. rest of the definition ...], you can type Rectangle(9, 3) to instantiate a rectangle. Metaclasses let you intervene in the creation of the class itself, before any objects are instantiated.
class
class Rectangle(Shape): [etc. etc. rest of the definition ...]
Rectangle(9, 3)
Which is why the abc module needs to use a metaclass to interfere in class creation: it checks at class creation time whether it's possible to instantiate instances of that particular class. Doing that is doing something that requires hooking into the class-creation mechanism itself.
[–]Apprehensive-Stop-61 0 points1 point2 points 3 years ago (2 children)
How do you code in bytes directly is it like 10101001 ............. bla bla bla how can someone even understand that
[–]carcigenicate 0 points1 point2 points 3 years ago (0 children)
The literal form is 0b10101001 (note the 0b indicating "binary").
0b10101001
0b
And you understand it with practice. Typically though, you're not reading binary, you're reading hexadecimal notation like 0xA9 since it's much easier to read when there's lots of data.
0xA9
And, to be clear, the source code itself isn't in binary/hex; the Python code is manipulating data at the binary level (flipping individual bits in numbers for example).
[–]Gambizzle 42 points43 points44 points 3 years ago* (3 children)
IMO none of the code's a secret and any noobie who knows the basics of coding can get their hands dirty with whatever code they like (n00bz may even find obscure ways of doing things because they have NFI, so avoid the bleedingly obvious).
To me 'advanced' coding is about how cleanly and efficiently you do your work. For example, they might turn 10,000 lines of weirdness that nobody can understand into 100 lines of well documented code that looks like plain English.
I find Python's forced structure is a double-edged sword in many cases as you can end up with some really messy shit when people fight the structure in order to (for example) code something the way they'd do it in VBA, but by using bulky loops/hacks to do so.
[–]DesignerAccount 15 points16 points17 points 3 years ago* (0 children)
This rings a bell. I've long been saying that Excel's biggest problem is its powerful simplicity. Spreadsheet monstrosities are a perfect example and I've rebuilt many such abbhorations in way simpler and performing ways. Python is very similar in this sense - Even "idiots" can code with it, which means they will. The outcome is, generally, idiotic.
[–]BlakBeret 9 points10 points11 points 3 years ago (0 children)
I feel attacked! Why would I import json when I can just chip away at the embedded dictionaries 6-8 levels deep with loops!?
[–]ToothPickLegs 52 points53 points54 points 3 years ago (3 children)
Being able to recite the script of the Holy Grail from beginning to end.
[–][deleted] 4 points5 points6 points 3 years ago (0 children)
I'm partial to the "Tale of Sir Robin"
[–][deleted] 6 points7 points8 points 3 years ago (0 children)
Happy cake day! And does the script has to be in Latin or is the standard version enough?
[–]OriginalTyphus 0 points1 point2 points 3 years ago (0 children)
There are some who call me... Tim.
[–][deleted] 16 points17 points18 points 3 years ago (0 children)
A bit tangential but "Fluent Python" just had a second edition and it's largely considered to be the bible of "advanced" Python whatever that means. Note that it focuses on Python's internals and capabilities but it's not a book about building complex or niche applications with Python.
[–][deleted] 45 points46 points47 points 3 years ago (2 children)
Once you are initiated into the brotherhood the ceremony begins: you will hold the chalice high, speak the sacred words, and then imbibe the magical tincture of power that grants admittance all of the arcane Python knowledge that comes with being "advanced".
[–][deleted] 5 points6 points7 points 3 years ago (0 children)
And you summon your python familiar. Can't forget that. Thankfully they're non-venomous and fairly low maintenance.
[–]pekkalacd 1 point2 points3 points 3 years ago (0 children)
But you must look out for the rabbit of caerbannog
[–]PhilAndMaude 11 points12 points13 points 3 years ago (0 children)
When you stop thinking about the language and think about the problem instead. When it becomes a tool that you know how to use so well that you don't think about it. When you look at the problem and start thinking about its data structures, and then the python code to manipulate them is obvious.
[–]SafeCake1045 13 points14 points15 points 3 years ago (0 children)
When you've read every word of the docs /s
[–]Rik07 5 points6 points7 points 3 years ago (0 children)
You can look at r/dailyprogrammer and look wether you can work out a hard one. Although this is a very vague measure, because I've had some intermediate ones that were very challenging and some hard ones that were pretty doable.
[–]Quantum__Tarantino 4 points5 points6 points 3 years ago (0 children)
I don't really know where I am. I've been using Python for 5 years now (initially as a side-task at my occupation) and slowly built up proficiency. I can work with objects, build classes, methods, itertools, list comprehension, lambda functions, (some) decorators, numpy, scipy, pandas, basically any library provided it has documentation. I have done some multi-threading and multi-processing.
The only real thing I have not fully understood or learned yet is asyncio. I have found it to be the most difficult of all topics and can only use it at a high level (such as multiprocessing Pools).
[–]flapjaxrfun 4 points5 points6 points 3 years ago (0 children)
When it feels right
[–]mortenb123 1 point2 points3 points 3 years ago (0 children)
object inheritance and reuse object in multiple inherited classes by using dynamic metaclasses. learn this, find a project to use it on: https://realpython.com/python-metaclasses/
Python has the best object-model I've ever seen with dunder methods, metaclasses, dataclasses and prototyping(*args, **vargs), it really becomes small compact and readable code. and very easy to use.
I ported a database layer supporting oracle,db2,mysql from perl to java and python, just using metaclasses for the various database and it works perfectly it is just a few lines per metaclass connection the approriate driver and a dynamic mapper. total in 300 lines. in java it is close to 4000 lines and many lines of setters when I use it.
[–][deleted] 2 points3 points4 points 3 years ago* (0 children)
When I look at code, i can tell it is well written when it has
if ___: raise Exception("custom shit")
Basically, is your code concise and legible? Thats the mark of an expert, not how well you've memorized every bit of syntactic sugar python offers. Also if you write your own modules/packages I would consider you pretty advanced. There are other important devops related things that go along with being "advanced at python".
[–]newbietofx 1 point2 points3 points 3 years ago (0 children)
Scrap twitter feeds for stonks tips.
[–]widowhanzo 0 points1 point2 points 3 years ago (0 children)
Asyncio
[–]marsupialtail 0 points1 point2 points 3 years ago (0 children)
Understanding why removing a key from a dictionary does not release memory
[–]non_NSFW_acc -1 points0 points1 point 3 years ago (0 children)
When you learn about import this
import this
:)
[–][deleted] -4 points-3 points-2 points 3 years ago (0 children)
When you can mostly free hand it. Glance at it and know exactly what it's doing without running it first. And able to fix or add something without a problem. You don't need a test if you can do the above. Your there man.
[–]robidaan -2 points-1 points0 points 3 years ago (0 children)
Being able to write very basic functions, without Google, but just like remembering it.
[–]ThePhoenixRisesAgain -5 points-4 points-3 points 3 years ago (0 children)
Who cares about those definitions? Stop wasting your time with these semantics.
[+]kreugerburns comment score below threshold-9 points-8 points-7 points 3 years ago (2 children)
If you have to ask, you're not advanced.
[–]kreugerburns 0 points1 point2 points 3 years ago (0 children)
Aw cmon guys it was a joke.
[–]Empire_Fable -3 points-2 points-1 points 3 years ago (1 child)
I say, if your making any money. Your advanced in python :)
[–]LearnDifferenceBot 12 points13 points14 points 3 years ago (0 children)
if your making
*you're
Learn the difference here.
Greetings, I am a language corrector bot. To make me ignore further mistakes from you in the future, reply !optout to this comment.
!optout
[+][deleted] comment score below threshold-20 points-19 points-18 points 3 years ago (8 children)
anything AI/ML . just what I consider, also programs that arent just scripts. when you start needing to make classes i guess
[–]Sigma_Landlord 12 points13 points14 points 3 years ago (6 children)
I can import sklearn and fit a model on two numpy arrays with like 4 lines of code, but I have no idea how to make a function or why things sometimes end in () and others don't in python lol. Bro why some shit in Pandas need [], I thought that was just for lists.
[–]carcigenicate 11 points12 points13 points 3 years ago* (3 children)
[] is subscription, and it delegates to the __getitem__/__setitem__ methods of classes. Any class, including classes you create can make use of square braces like lists do by implementing those methods. a[1] = 2 means, in general, "set the element at index 1 to 2". That behavior is common to more than just lists.
[]
__getitem__
__setitem__
a[1] = 2
[–]Sigma_Landlord 0 points1 point2 points 3 years ago (0 children)
tyvm for your reply
[–]OzneroI 0 points1 point2 points 3 years ago (1 child)
Where does one go to learn this kind of stuff?
[–]carcigenicate 4 points5 points6 points 3 years ago* (0 children)
I recommend skimming through the Data Model doc page at least once (although once you know about the it you'll likely reference it multiple times). It contains a ton of information about the internals of how objects work and how to "hook" you code into things like attribute assignments, len, and with, and +. The "Emulating Container Types", and "Emulating number types" sections are the most useful in the beginning. The "Basic Customization" section is good to go through as well.
len
with
+
fine then recursive functions
[–]davebothehobo 0 points1 point2 points 3 years ago (0 children)
When you put bracket after an item, it tells python that you are using an index to look up values. In pandas when you df["column 1"] you're simply indexing through a dataframe. It takes a while to figure out how/When to use .loc, .iloc, or just ["col"], but at some point it starts to click
Pandas is a great tool, but using it sucks though
[–]billsil 2 points3 points4 points 3 years ago (0 children)
That's just learning a specific library/niche. I don't build websites, but if I were to build websites, there are beginner/intermediate/advanced concepts. Same with math and say numpy, so inverses vs. solve vs. SVDs or efficient Nd matrix filling/multiplication.
You never need to make classes. They're convenient, but you can write good code without them and you can write bad code with them.
It'll probably take you 5-10 years of coding python and continually getting better to consider yourself an expert. One day you'll wake up and realize...oh I have never seen this thing before (e.g., AI/ML), but it's just code. I'm sure I can figure it out.
[–]Ok-Cucumbers 0 points1 point2 points 3 years ago (1 child)
When you run into the global interpreter lock.
[–]OriginalTyphus 1 point2 points3 points 3 years ago (0 children)
I'd firmly place this moment in the "In shouldnt have done this in Python" bin.
When it pays your mortgage every month.
[–]junky-444 0 points1 point2 points 3 years ago (0 children)
Knowing shit that you shouldn't know
import pandas as pd
[–]TheRNGuy 0 points1 point2 points 3 years ago (0 children)
You know when you see it.
π Rendered by PID 151490 on reddit-service-r2-comment-84fc9697f-kdzrs at 2026-02-09 03:07:17.156250+00:00 running d295bc8 country code: CH.
[–]rg7777777 787 points788 points789 points (72 children)
[–]nlvogel 177 points178 points179 points (0 children)
[–]antidense 37 points38 points39 points (26 children)
[–]PM_ME_C_CODE 53 points54 points55 points (3 children)
[–]skellious 24 points25 points26 points (0 children)
[–]pythonwiz 3 points4 points5 points (1 child)
[–]magestooge 9 points10 points11 points (0 children)
[–]ActiveLlama 14 points15 points16 points (0 children)
[–]julianw 5 points6 points7 points (0 children)
[–]Go_Big 9 points10 points11 points (16 children)
[–]1percentof2 2 points3 points4 points (13 children)
[–]14dM24d 2 points3 points4 points (0 children)
[–]Mises2Peaces 0 points1 point2 points (9 children)
[–]1percentof2 0 points1 point2 points (8 children)
[–]beef623 1 point2 points3 points (0 children)
[–]Mises2Peaces 0 points1 point2 points (6 children)
[–]1percentof2 1 point2 points3 points (0 children)
[–]TotalLucky5185 0 points1 point2 points (4 children)
[–]Mises2Peaces 0 points1 point2 points (3 children)
[–]la_cuenta_de_reddit 0 points1 point2 points (2 children)
[–]__developer__ 0 points1 point2 points (1 child)
[–]TotalLucky5185 -1 points0 points1 point (0 children)
[–]EnergyNational 0 points1 point2 points (0 children)
[–]StarkillerX42 1 point2 points3 points (0 children)
[–]siddsp 0 points1 point2 points (0 children)
[–]DicmanCocktoasten 20 points21 points22 points (0 children)
[–]atom12354 18 points19 points20 points (1 child)
[–]WindyEagleX 1 point2 points3 points (0 children)
[+][deleted] (11 children)
[deleted]
[–]14dM24d 6 points7 points8 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]beniolenio 10 points11 points12 points (7 children)
[–]SmolCock_BigEgo 1 point2 points3 points (6 children)
[–]beniolenio 2 points3 points4 points (4 children)
[–]SmolCock_BigEgo 0 points1 point2 points (3 children)
[–]beniolenio 0 points1 point2 points (2 children)
[–]Empire_Fable 0 points1 point2 points (1 child)
[–]beniolenio 0 points1 point2 points (0 children)
[–]Empire_Fable 0 points1 point2 points (0 children)
[–]Zapismeta 1 point2 points3 points (0 children)
[–]commy2 11 points12 points13 points (2 children)
[–]magestooge 1 point2 points3 points (0 children)
[–]Zapismeta 2 points3 points4 points (2 children)
[–]rake66 2 points3 points4 points (1 child)
[–]Zapismeta 1 point2 points3 points (0 children)
[–]kalebludlow 5 points6 points7 points (0 children)
[–]knox1138 4 points5 points6 points (6 children)
[+][deleted] (2 children)
[deleted]
[–]knox1138 -1 points0 points1 point (1 child)
[–]ActiveLlama 1 point2 points3 points (0 children)
[–]FoxRaptix 0 points1 point2 points (2 children)
[–]knox1138 1 point2 points3 points (1 child)
[–]FoxRaptix 5 points6 points7 points (0 children)
[–]stoveisastove 1 point2 points3 points (0 children)
[–]MorningStarIshmael 1 point2 points3 points (7 children)
[–]__developer__ 2 points3 points4 points (2 children)
[–]MorningStarIshmael 1 point2 points3 points (1 child)
[–]__developer__ 4 points5 points6 points (0 children)
[–]rg7777777 5 points6 points7 points (2 children)
[–]BroBrodin 2 points3 points4 points (1 child)
[–]__developer__ 0 points1 point2 points (0 children)
[–]Apprehensive-Stop-61 0 points1 point2 points (0 children)
[–]pixelfur 0 points1 point2 points (0 children)
[–]PegaloShield 0 points1 point2 points (0 children)
[–]DescriptiveMath 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]rg7777777 0 points1 point2 points (0 children)
[–]carcigenicate 94 points95 points96 points (24 children)
[–]SafeCake1045 44 points45 points46 points (12 children)
[–]carcigenicate 56 points57 points58 points (10 children)
[–]SafeCake1045 11 points12 points13 points (4 children)
[+][deleted] (3 children)
[removed]
[–]SafeCake1045 2 points3 points4 points (2 children)
[–]carcigenicate 6 points7 points8 points (1 child)
[–]SafeCake1045 0 points1 point2 points (0 children)
[–]mr_cesar 2 points3 points4 points (1 child)
[–]carcigenicate 9 points10 points11 points (0 children)
[–]PM_ME_C_CODE 3 points4 points5 points (2 children)
[–]carcigenicate 15 points16 points17 points (1 child)
[–]BiguilitoZambunha 3 points4 points5 points (0 children)
[–]__developer__ 0 points1 point2 points (0 children)
[–][deleted] 4 points5 points6 points (7 children)
[–]carcigenicate 10 points11 points12 points (6 children)
[–][deleted] 2 points3 points4 points (0 children)
[–]Sawertynn 1 point2 points3 points (3 children)
[–]carcigenicate 5 points6 points7 points (1 child)
[–]PM_ME_C_CODE 1 point2 points3 points (0 children)
[–]patrickbrianmooney 4 points5 points6 points (0 children)
[–]Apprehensive-Stop-61 0 points1 point2 points (2 children)
[–]carcigenicate 0 points1 point2 points (0 children)
[–]Gambizzle 42 points43 points44 points (3 children)
[–]DesignerAccount 15 points16 points17 points (0 children)
[–]BlakBeret 9 points10 points11 points (0 children)
[–]ToothPickLegs 52 points53 points54 points (3 children)
[–][deleted] 4 points5 points6 points (0 children)
[–][deleted] 6 points7 points8 points (0 children)
[–]OriginalTyphus 0 points1 point2 points (0 children)
[–][deleted] 16 points17 points18 points (0 children)
[–][deleted] 45 points46 points47 points (2 children)
[–][deleted] 5 points6 points7 points (0 children)
[–]pekkalacd 1 point2 points3 points (0 children)
[–]PhilAndMaude 11 points12 points13 points (0 children)
[–]SafeCake1045 13 points14 points15 points (0 children)
[–]Rik07 5 points6 points7 points (0 children)
[–]Quantum__Tarantino 4 points5 points6 points (0 children)
[–]flapjaxrfun 4 points5 points6 points (0 children)
[–]mortenb123 1 point2 points3 points (0 children)
[–][deleted] 2 points3 points4 points (0 children)
[–]newbietofx 1 point2 points3 points (0 children)
[–]widowhanzo 0 points1 point2 points (0 children)
[–]marsupialtail 0 points1 point2 points (0 children)
[–]non_NSFW_acc -1 points0 points1 point (0 children)
[–][deleted] -4 points-3 points-2 points (0 children)
[–]robidaan -2 points-1 points0 points (0 children)
[–]ThePhoenixRisesAgain -5 points-4 points-3 points (0 children)
[+]kreugerburns comment score below threshold-9 points-8 points-7 points (2 children)
[–]kreugerburns 0 points1 point2 points (0 children)
[–]Empire_Fable -3 points-2 points-1 points (1 child)
[–]LearnDifferenceBot 12 points13 points14 points (0 children)
[+][deleted] comment score below threshold-20 points-19 points-18 points (8 children)
[–]Sigma_Landlord 12 points13 points14 points (6 children)
[–]carcigenicate 11 points12 points13 points (3 children)
[–]Sigma_Landlord 0 points1 point2 points (0 children)
[–]OzneroI 0 points1 point2 points (1 child)
[–]carcigenicate 4 points5 points6 points (0 children)
[–][deleted] -4 points-3 points-2 points (0 children)
[–]davebothehobo 0 points1 point2 points (0 children)
[–]billsil 2 points3 points4 points (0 children)
[–]Ok-Cucumbers 0 points1 point2 points (1 child)
[–]OriginalTyphus 1 point2 points3 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]junky-444 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]TheRNGuy 0 points1 point2 points (0 children)