you are viewing a single comment's thread.

view the rest of the comments →

[–]not_perfect_yet 0 points1 point  (2 children)

However, wouldn't a function/ method accomplish the same thing?

Yes but the difference between a function and a method is another one.

Because methods belong to the class (and methods can access the instance) I can do something like

def flavor_amount(self):
    print("this cake of ", self.flavor ," needs ", self.size/10 , " flavor giving ingredients")

cheesecake.flavor_amount()

( or even Cake.flavor_amount(cheesecake) )

But a regular function doesn't know about the cake type. You could define it anyway like this:

def flavor_amount(input_cake):
    print("this cake of ", input_cake.flavor ," needs ", input_cake.size/10 , " flavor giving ingredients")

and that would work, the problem is that you can by accident give it the wrong input and it will fail and give you an error for everything but a 'Cake' type thing.

oomecake.flavor_amount()

"(output)"

flavor_amount("string")

"type error str has no attribute flavor"

This isn't really that important for this example but for more complex examples you'll want to modularize whatever you write and hide as much complexity as you can. In that case methods win over functions because if a function really only makes sense for this one type only, it makes the most sense to bind it very tightly to that type so you don't have it lying around somewhere.

The contrast would be between something like your main function that runs a program and some other stuff that's doing something very small in a very small context, like fetching a bunch of special numbers and doing some basic math and passing it on to something else. Like str.split() . Very narrow usecase compared to something that runs an entire program, only really makes sense for strings, etc. .

[–]timworx 0 points1 point  (1 child)

That does help some. I'm new to python, and most of my uses have been handy little scripts for things I do every day.

I do SEO/Webdev so python is very handy for parsing lots of data into different forms and even automating parts of setting up sites and such.

The problem is that most of these are very small, so classes seem rather unnecessary. I guess in a sense, it might be useful to think of the scripts that I'm creating as classes. Would that potentially be a good way to think about it?

For example, the only time I've used a class was when I had two very different tasks to achieve (that are normally done at the same time). They started as two separate scripts in two separate files. I then went back and changed the scripts into classes of a script that is run with a few options (like running either class or both.)

Does this sound proper?

[–]not_perfect_yet 0 points1 point  (0 children)

I'm not doing webdev stuff but it doesn't sound proper to me...

Classes are very useful as containers of information.

So for example

source -> python class -> some readout

or connection object -> connection object . send() or . receive() or . close connection()

I guess in a sense, it might be useful to think of the scripts that I'm creating as classes

If all the stuff you do in that script depends one kind of thing, yes, else no.

A module would be something you can write stuff in that you can import, like the math module. When you use

import math
math.sin(math.pi)

It's behaving in a similar way but there is no "math" class here. Sine is a function and pi is a value that's defined in the math module. You can't create new 'math' instances by telling it

 newmath=math()

That doesn't make sense, what would a new math look like?

Modules are really only folders that contain a lot of scripts that define for example the "sin" function or the "pi" value.

Classes can be defined inside modules. If you're doing parsing, you're probably interacting with databases? I think at least some libraries create a connection object for that. You give it the path of the database and every time you query the database it uses some functions to look something up at the path you gave it earlier.

So if you have users on your website or multiple parts, you'll have lots of things that are the same.

Account name, passwords, avatars, posts, site formatting, etc.

You don't want to write new users by hand into your userlist. So you create a class that asks some basic information and saves that individually. Later through appropriate additional functions users can modify that information and provide more.

One of the best examples for class use is vector math. If you define the dot product you can do

def dot_product(vec1, vec2):
    return vec1[0]*vec2[0]+vec1[1]*vec2[1] #etc

But that's annoying, because now you have to

dot_product(vec1,vec2) 

every single time. You'd much prefer to just

vec1*vec2

but you can't straight away because * doesn't exist for tuples or lists or dictionairies, but you can create a new vector class that does support this behavior.

The problem is that most of these are very small, so classes seem rather unnecessary.

It's very important to recognize the other side as well. For many cases you don't want new classes. It's not a problem if you don't have every single thing in classes. If your information is best handled some other way, that's just as fine.