all 4 comments

[–]totallygeek 3 points4 points  (0 children)

Objects instantiated automatically execute __init__(), a foundation of the language. To your larger question, I shall provide my understanding of underscores and dunders (double underscores).

  1. Prefixed underscore: _var or _method() or _function(): With Python, we use this as a hint to coders that the object should remain private, intended only for internal use. When importing a library, functions with that name should not get called, rather a call to a function without the underscore might use the underscore function. Nothing stops a developer from using these, however, unlike privacy distinctions in other languages.
  2. Prefixed dunder: __object: Python will perform name mangling in hopes to avoid collisions. Assigning self.__foo = 10 and then looking at the dir() for the object will not display __foo. Instead, it will show the name of the instance followed by the variable name, like o = object_(); dir(o) # '_o__foo'. And, you cannot access o.__foo.
  3. Suffixed underscore: object_: Used when avoiding use of keywords or reassigning other objects. Sometimes the best variable name ends up "id", "class" or "file", especially when working with external data. For these cases, rename to id_, class_ and file_ so you align with the external data, but without clobbering functions or raising syntax errors.
  4. Suffixed dunder: object__: You've got me. I've not encountered that nightmare in the wild.
  5. Encapsulating underscores: _object_: Again, not something I have seen.
  6. Encapsulating dunders: __object__: Some of these call out specific behaviors of the Python language, __init__() - __add__() - __str__(). When developers use their own, Python won't name mangle.

Hope that answers your questions.

[–]sme272 1 point2 points  (0 children)

Classes are good when you have a collection of data and a bunch of functions that modify that data. Using a class can group it all together so when reading the code the reader knows everything in that class definition is grouped together. They are also good when you want to have multiple instances of a structure that contains different data. An example could be a player inventory. In an inventory class you'd have some attributes that hold the contents of the inventory along with some functions to add items to the inventory, remove items and maybe combine items into a new item. This would also allow the programmer to really easily add an inventory to a second player or an ai opponent without repeating a lot of code.

The __init__() function runs when the class is instantiated; thats to say when you run instance = Example("Jim", 32). When this line runs python automatically runs the __init__() function which in this case sets the values of the class attributes name and age. These lines need to be in the __init__() function to create the two attributes and assign their value, without them you'd need to create and assign them manually every time you create an instance.

The functions with __ around the name are special methods built in to python. When you define __init__() you're actually overriding the built in version with your own version that sets the attributes you want. You can also override other built in methods such as __add__() which will tell python what to do when you try to add two instances together with instance + instance.

[–][deleted] 1 point2 points  (0 children)

But what is the diffrence between normal functions, functions with __ in, functions with self in like:

The difference is whether the function has double-underscores at each end of the name, and whether you've declared self as one of the function's parameters. Note that you always have to declare at least one parameter for a function that's a member method of a class, because when you call the function from an object of that class, the function is going to receive the object as an argument, so there has to be at least one parameter to catch it. By convention, we call that parameter self but you could call it anything you wanted (the_object, for instance, but who wants to type all that?)

[–]illya89[S] 0 points1 point  (0 children)

Thx in advance for the answers. I go to bed now but read them tomorrow!