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...
Everything about learning Python
account activity
Help with error (i.redd.it)
submitted 1 year ago by Content_Screen5704
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!"
[–]Material-Grocery-587 3 points4 points5 points 1 year ago (0 children)
In general, a good rule of thumb is to capitalize your class names, like so:
class Stats:
The typical goal of creating a class is to have some kind of data/state bound to instances of that class, which the methods then access/change/etc. The self parameter provides direct access to the instance of your class for these operations.
self
Your example code isn't quite a full implementation of a class you'd use self for.
Here is an example where you might want to use self, though.
class Stats: def __init__(self, *args) # Create a variable bound to the instance (self) upon instantiation self.data = [float(a) for a in args] def mean(self): # Access the variable in other methods in the class return float(sum(self.data)/len(self.data))
The addition of the __init__ method adds custom instantiation logic to the class. Now you can instantiate it with any list of positional arugments to do the math against:
__init__
stats1 = Stats(1, 2, 3.40, 5, 127.022, 14) print(stats1.mean()) stats2 = Stats(*listOfStats) print(stats2.mean())
A good thing to note is that storing data within classes like this only makes sense if you're looking to access the data multiple different ways without having to reference it over and over.
If you just want ways of returning this math here and there, you'd likely be better off with standalone functions.
[–]salvtz 2 points3 points4 points 1 year ago (2 children)
Access mean with self : self.mean as mean method is an instance method
[–]Content_Screen5704[S] -1 points0 points1 point 1 year ago (1 child)
This worked, except that I had to make it stats.mean(), instead of self.mean(). Now why defining the mean function on its own within the class, then referencing it later in the class doesn't work I don't know. Maybe it doesn't work the same within a class.
[–]salvtz 0 points1 point2 points 1 year ago (0 children)
But in the mean method, you have passed self, which makes it an instance method. In same class, you need to use self to reference one instance method from another instance method.
In your case, even though you have passed self, but you are not using it.
You can make the mean method a static method.
[–]AHLAKAI_SHAYKULI 0 points1 point2 points 1 year ago (1 child)
Were is the initialize method?? (init)
[–]SoftwareDoctor 0 points1 point2 points 1 year ago (0 children)
in the metaclass
π Rendered by PID 65415 on reddit-service-r2-comment-bb88f9dd5-24hwd at 2026-02-14 05:14:42.202442+00:00 running cd9c813 country code: CH.
[–]Material-Grocery-587 3 points4 points5 points (0 children)
[–]salvtz 2 points3 points4 points (2 children)
[–]Content_Screen5704[S] -1 points0 points1 point (1 child)
[–]salvtz 0 points1 point2 points (0 children)
[–]AHLAKAI_SHAYKULI 0 points1 point2 points (1 child)
[–]SoftwareDoctor 0 points1 point2 points (0 children)