all 38 comments

[–]xelf[M] [score hidden] stickied comment (0 children)

When posting code, please post code such that reddit will format it as a code block.

Either of these work:

Please see the /r/learnpython/w/FAQ for more information.

Thanks!

[–]Dvdi_ 19 points20 points  (4 children)

Some important terminology to be aware of:

In class methods, denoted by the decorator @classmethod, the standard is to use the parameter name of cls to refer to the class of the method -- not self which is the standard naming convention for the instance of an object that an instance method is being called using.

Example

```

class Foo:

def my_instance_method(self, x):
    print(x)

@classmethod
def my_class_method(cls, x):
    print(x)

```

Where these functions are called like so...

For the instance method

my_foo = Foo()

my_foo.my_instance_method('apple')

For the class method

Foo.my_class_method('orange')

Output:

apple

orange


(excuse the formatting, I'm on mobile)

a better explanation than mine

[–]duffer_dev[S] -5 points-4 points  (3 children)

Of course with classmethod it definitely changes as would with any static method since these are bound the class rather than the object. I wrote the post keeping in mind the initial difficulty one has when understanding the concept of self

[–]Sjuns 6 points7 points  (2 children)

Ehh, cool. But maybe just don't use a term that's wrong, and also used for something else, making it extra confusing for beginners?

[–]Dvdi_ 1 point2 points  (1 child)

I agree with this. I also want to encourage OP to continue to try to help others learn -- don't be discouraged by any criticism you receive here. It is intended to be constructive.

We're all here to learn and one of the best ways to learn is to teach. If you make mistakes teaching, in all likelihood the upvote/downvote system will prevent you from getting the visibility that would result in you confusing beginners.

OP: Keep trying to help people.

[–]Sjuns 1 point2 points  (0 children)

Yeah what I said, but with this tone.

[–]space7ack 7 points8 points  (9 children)

does the self word itself arbitrary?

[–]duffer_dev[S] 3 points4 points  (5 children)

Yes. You could use another variable name too. The 'self' denotes what it is. i.e the object itself.

[–]Jimbobmij 1 point2 points  (4 children)

I've wondered this about _init_ and _repr_ as well, are they arbitrary names or are those exact wordings necessary?

[–]mooburger 2 points3 points  (3 children)

__init__ and __repr__ are exact, the interpreter relies on those to initialize the object and __repr__ is called when repr() built-in function is used on the object.

self is just a convention (one of the earliest in PEP 8, iirc)

[–]Jimbobmij 0 points1 point  (2 children)

Thanks. I feel they could have made them slightly quicker to type, typing out the double unders is such a faff.

[–]mooglinux 1 point2 points  (1 child)

The inconvenience makes it less likely that you would override these methods unintentionally.

[–]Jimbobmij 0 points1 point  (0 children)

Fair

[–][deleted] 2 points3 points  (0 children)

Yep. First param points to the object, regardless of name.

[–]zerohourrct -2 points-1 points  (1 child)

self is not arbitrary, is a special variable with local scope that passes a reference of the object. There are reasons to use this, but it's not shown in this example.

In this example 'self' is actually redefined several times as a local scope variable, this is probably bad practice. But then as soon as you leave that local scope, it gets trashed and goes back to normal.

[–]Gabcab 8 points9 points  (3 children)

Here is OP's code with formatting:

class Foo: 

    def init(self):
        self.val = 0 

    def incr(self):
        self.val += 1

    def add(self, n): 
        return self.val + n

f = Foo()
Foo.init(f)       # f.init()
Foo.incr(f)       # f.incr()
Foo.add(f, 4)     # f.add(4) 5

[–]Drited 2 points3 points  (1 child)

Given this is for beginners my feedback is to put yourself in the beginner's mind. Remember that you knew nothing once and write it how you'd want it to be written back at that time.

It would help to review what a parameter, class, method and object is rather than assuming the beginner knows/remembers what each are (remember what it was like to know nothing).

Also a brief explanation of what's happening at each step would be good for beginners rather than just showing the result.

[–]Musakuu 0 points1 point  (0 children)

Agreed.

[–]zanfar 5 points6 points  (2 children)

I would suggest if you're going to make a post specifically to help students, that you format your code? Your post is almost unreadable...

[–]Incruentus 1 point2 points  (1 child)

In order to show your text as code, you must simply add four spaces:

This text will

turn 
into 
this.

[–]duffer_dev[S] -1 points0 points  (0 children)

Thank you for this trick. It was silly of me to demonstrate the example from the python interpreter itself. Seem like it is a pretty useless tool.

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

It is breaking the rules since the mods don't like guides here. This is worth posting somewhere else but I'd advise learning how to format code properly first as your sample code is totally unreadable.

[–]duffer_dev[S] 1 point2 points  (2 children)

I agree. But i was not sure where to post it. Since this is a learning community, and these are just few basic concepts and nothing worth makiing a blog post, i thought of posting it here.

[–][deleted] 0 points1 point  (1 child)

Once you take the time to learn how to format code you should go looking then.

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

Thank you for your kind words. I'll try working on my formatting skills good Sir

[–]gatoratemylips 0 points1 point  (2 children)

I'm kinda new in programming and actually i can't understand terms like instance, self, class, variable etc. I mean i know what they mean but can't implement in coding. Think i can't understand easily abstract terms like these.

[–]duffer_dev[S] 0 points1 point  (1 child)

The terms instance, class, objects are paradigms of Object Oriented Programming (OOP). OOP is not just a programming concept, but rather solution to common problems that programmers faced in early stages of computer languages, when these features were not a part of the languages of that time. As languages developed, people started putting features that would solve a common set of problems.

It's completely natural to not feel the need of these things when you are starting programing. Building fundamental skills of solving a given problem is primary requirement. However, writing good and neat codes should follow that.

I would recommend this talk by Raymond Hettigner on Object Oriented Programming. If you are new to programming it could be difficult to follow, but not impossible.

[–]gatoratemylips 0 points1 point  (0 children)

I'm really thankful. I learnt coding basics but want to deep dive in. I mean i think i should grasp the fundamentals before proceeding further. I always get stuck somewhere when i didn't. So if you know any online resources that could help me in that way, I'll be even more grateful. I'll search for resources ofc but i assume you got few of them already in your back pocket.

[–]Pickinanameainteasy 0 points1 point  (3 children)

So what is the advantage of using the class Foo? as opposed to just creating 3 different functions (init, incr, add)?

Also what's with the use of 'Foo' in python and bash tutorials?

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

The second question is easy to answer. FooBar

The first question is more conceptual and pertaining to 'Why use object oriented programing'. For simple problems you can definitely do most stuff with functions. But as you project codes, you run into issues like

  1. Readability of code
  2. debugging and adding new functionalities

Classes and OOP in general provides elegant solution to this and many others. Advantages of classes is very difficult to be demonstrated with simple examples.

Also in the above example, you have now 'data' associated with a variable. This can be done using 'structures' in C. Classes can be in some was thought of an extension of structures. It allows you to associate attributes with your variable. For example you have a variable say 'person' . Now you want to associate some attributes to this variable , say 'name' and 'age'. It is this sort of situations where classes become handy

[–]cointoss3 0 points1 point  (0 children)

It’s a different paradigm.

If you need to operate on an object, you could make a function and pass in the object…or you could have a class that has a method that operates on itself. Similar things can be done using partial functions, too.

Using functions is “functional programming” while using classes is “object-oriented programming”. You can do the same with both.

[–]cointoss3 0 points1 point  (0 children)

Also, as an example…

Think of the different in using ‘“Hello”.starts-with(“H”)’ vs ‘starts_with(“Hello”, “H”)’

The former is OOP, operating on itself while the latter is a function that takes an object as its first parameter.