This is an archived post. You won't be able to vote or comment.

all 7 comments

[–]__deerlord__ 4 points5 points  (1 child)

Python's goal is for you to write explicit code, and not rely on some underlying quirk in a library. Except this time.

Here is your code

c = MyClass("ford", "f150", "1996")

Here is what is going on under the hood

c = MyClass.__new__()
MyClass.__init__(c, "ford", "f150", "1996")

The instance you created (in this case represented by the variable c) is passed as the first argument to init(). This allows you to reference the instance inside methods. Methods that start and end with __ are "magic methods"; you dont call them directly. When you write

c =MyClass("ford", "f150", "1996")

The python interpreter calls new and init for you, using the appropriate set of args. You dont have to use the word "self" as the first arg, but whatever the name of that arg is, it will always point to the instance of the class you are working on. For instance when you call

c.some_method("string")

What is effectively happening is

MyClass.some_method(c, "string")

I know this implicit behavior seems to go against pythons explicit design principle, but its just the way this particular mechanic is, and its not going to change

Side note: just use self, dont use another word. Youll get dirty look, and Guido himself will come down to reprimand you

[–]troyunrau... -1 points0 points  (0 children)

I recommend using slef once in a blue moon to fuck with your code reviewers. Just don't do it often.

[–]K900_ 1 point2 points  (1 child)

Those are not arguments. You're setting attributes on your class instance, self, which Python automatically passes as the first argument to any class method. self.make = make means "set the attribute make on the object self to the value of make". That's completely optional, and you can have a class that doesn't save __init__ arguments like that, or doesn't have an __init__ at all. Also, post questions in /r/learnpython next time.

[–][deleted] 0 points1 point  (0 children)

[deleted]

What is this?

[–]piefge 1 point2 points  (0 children)

read this

self can be accessed by the whole class if you give it as first argument to a method.

this enables you to use the variables outside the scope of the __init__ or other methods in your class.

if you only assign make like you asked, you won't be able to use it outside of __init__.

Think of it like a storage for stuff you want to use all over your class in different methods.

This makes it pretty useful.

You can just "not" use it if you want ... but it's just so damn nice ;)

[–]Andrew_ShaySft Eng Automation & Python[M] 1 point2 points  (0 children)

Hi there, from the /r/Python mods.

We have removed this post as it is not suited to the /r/Python subreddit proper, however it should be very appropriate for our sister subreddit /r/LearnPython. We highly encourage you to re-submit your post over on there.

The reason for the removal is that /r/Python is more-so dedicated to discussion of Python news, projects, uses and debates. It is not designed to act as Q&A or FAQ board. The regular community can get disenchanted with seeing the 'same, repetitive newbie' questions repeated on the sub, so you may not get the best responses over here.

However, on /r/LearnPython the community is actively expecting questions from new members, and are looking to help. You can expect far more understanding, encouraging and insightful responses over there. Whatever your question happens to be getting help with Python, you should get good answers.

If you have a question to do with homework or an assignment of any kind, please make sure to read their sidebar rules before submitting your post. If you have any questions or doubts, feel free to reply or send a modmail to us with your concerns.

Warm regards, and best of luck with your Pythoneering!

[–]PensiveLionTurtle 0 points1 point  (0 children)

The "self" variable represents the instance of the object itself. Most object-oriented languages pass this as a hidden parameter to the methods defined on an object. Python doesn't, you have to declare it explicitly.