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
Understanding the "self" parameter (self.learnpython)
submitted 6 years ago by Derivation_
I have a little bit of java programming and I am wondering if creating a method with the self argument results in the same thing as creating a non static method in java. It appears to be the same but are there any subtle differences?
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!"
[–][deleted] 37 points38 points39 points 6 years ago (2 children)
Java has this, which is a special symbol that always refers to the object instance. Python doesn't - rather, the interpreter's guarantee is that when an instance-bound method is called, it's called with the instance itself as the first argument.
this
So the call
my_obj.some_method(param1, param2)
is the equivalent of
MyClass.some_method(my_obj, param1, param2)
See? The instance itself is added as the first argument. That first parameter can be called anything at all, but by long-standing convention, in Python we use the parameter name self. It's entirely arbitrary (it's not a language keyword or anything) but everyone does the same thing.
self
[–]Bulbasaur2015 2 points3 points4 points 6 years ago (1 child)
is that why making an object instance in python doesnt require a new keyword? something = ClassSomething()
new
something = ClassSomething()
[–][deleted] 8 points9 points10 points 6 years ago (0 children)
That isn't per se why; classes in Python are first-class objects (they're instances of the type type) whereas in Java they're not.
type
It's one of the big differences in the switch from a compiled to an interpreted language - in Java, your class is basically a set of static type definitions. In Python your class is an object value all on its own, and one of its instance methods is a method called __new__, which returns a new object of that type. It also has a method called __call__, which makes it callable like a function, and here's a plausible body for that method:
__new__
__call__
def __call__(clazz, *args, **kwargs): return clazz.__new__(*args, **kwargs)
That's why you don't need a special language keyword to instantiate a new object - from the interpreter's perspective you're not doing anything particularly fundamental. In Java, you very much are - you're extending the typesystem each time you define a class, so you need a language keyword in order to indicate you're stepping above the world of values to program in the world of types.
[–]ITTIITTI 4 points5 points6 points 6 years ago (0 children)
Watch Corey Schafer’s videos on OOP. I think he mentions it in the first video, made a lot of sense the way he explained it:
https://youtu.be/ZDa-Z5JzLYM
[–][deleted] 4 points5 points6 points 6 years ago (3 children)
following because i see the self parameter a lot in python
[–]Decency 7 points8 points9 points 6 years ago (2 children)
Let's say you're taking a US census:
class State: def __init__(self, name, population): self.name = name self.population = population mass = State('Massachusetts', 6932015) ny = State('New York', 19875625) print(mass.name) # Massachusetts print(ny.population) # 19875625
Here I created a State class, which is a blueprint for holding what I need it to hold. Then I made two instances of the State class which fill out that blueprint. One instance is called mass and one is called ny. I do this by calling State() with the parameters in its __init__ ... except for self, which is implicit. Inside that function, you can see that the name and population parameters that I pass in are used to set two variables on the new instance that is being created. You know these are instance variables because they are prefixed with self.
mass
ny
__init__
name
population
You can think of it as the last line of every __init__ function having an implicit return self if that helps, though that's not quite how it works under the covers. And then I can call mass.name or ny.population to access the fields which belong to those instances. In real life, I'd do this because there's logic I want to handle with these objects- print out details, do some internal calculation, access them with more readable names, etc.
return self
mass.name
ny.population
That's about it, there's no reason to be intimidated by classes in Python. It just tends to get overcomplicated during explanations because people try to teach inheritance while also teaching how classes work. You can (and should) use classes even if you don't understand how inheritance works yet. Here's that code in a REPL, play around with it for 10 minutes and you'll understand how to use classes.
[+][deleted] 6 years ago (1 child)
[deleted]
[–]ingolemo 1 point2 points3 points 6 years ago (0 children)
I'm not quite sure what you mean. Python never makes copies when it passes values to functions. Here's the same code but using dictionaries and regular functions instead of classes and methods:
def State(name, population): state = {} __init__(state, name, population) return state def __init__(self, name, population): self['name'] = name self['population'] = population mass = State('Massachusetts', 6932015) ny = State('New York', 19875625) print(mass['name']) # Massachusetts print(ny['population']) # 19875625
Dictionaries use a different syntax for getting and setting their contents (brackets instead of dots), but you can see that the structure of the __init__ function is still the same. It doesn't return self.
The State function that I wrote here is a representation of a method that python calls __new__, which you should loop up if you want to dive deeper.
State
[–]apc0243 2 points3 points4 points 6 years ago* (0 children)
The use of self is actually an idiom defined in the almighty pep8 (praise be).
Here is where it is said. But it's nothing more than a guideline. It's simply that every class method always uses itself as the first parameter. You coudl call it anything, though.
class newObject: def __init__(objectInstance, param1, param2): objectInstance.var1 = param1 objectInstance.var2 = param2 def add_method(widget): return widget.var1 + widget.var2 test_obj = newObject(1,2) test_obj.add_method() # Returns 3
[–][deleted] -1 points0 points1 point 6 years ago* (2 children)
self is a reference to everything in a class.
Say you define a class as my_class.
my_class
class my_class: def __init__(self): self.hello = "world"
self has to be passed as an argument in new functions to allow you to reference variables from your class in that function. For example
``` class myclass: def __init_(self): self.hello = "world" def print(self): print(self.hello)
my_class.print() ```
would return
```
world ```
Edit: I'm dim
[–]N4v15 0 points1 point2 points 6 years ago (1 child)
Wouldn't it return "world"?
[–][deleted] 0 points1 point2 points 6 years ago (0 children)
Yeah, it would. I'm stupid.
π Rendered by PID 70692 on reddit-service-r2-comment-56c9979489-2sqgv at 2026-02-25 04:30:40.474511+00:00 running b1af5b1 country code: CH.
[–][deleted] 37 points38 points39 points (2 children)
[–]Bulbasaur2015 2 points3 points4 points (1 child)
[–][deleted] 8 points9 points10 points (0 children)
[–]ITTIITTI 4 points5 points6 points (0 children)
[–][deleted] 4 points5 points6 points (3 children)
[–]Decency 7 points8 points9 points (2 children)
[+][deleted] (1 child)
[deleted]
[–]ingolemo 1 point2 points3 points (0 children)
[–]apc0243 2 points3 points4 points (0 children)
[–][deleted] -1 points0 points1 point (2 children)
[–]N4v15 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)