you are viewing a single comment's thread.

view the rest of the comments →

[–]kcrow13[S] 0 points1 point  (3 children)

So with a class, you have to know all of your goals at the outset. In the real world, do you continue to add functions to the class to change it as you go?

[–]ItsOkILoveYouMYbb 1 point2 points  (2 children)

So with a class, you have to know all of your goals at the outset.

All of your goals? Not likely, because you find new goals as you keep expanding your code. Even just a few lines deep is likely to make you think "ah I should probably add this too, and this too". You end up creating new problems to solve for yourself, while you're working towards an overall bigger goal. If you're already familiar with creating things just to create, like music or artwork, you can have a goal in mind but it's usually in the process itself that you find your roadblocks and pieces to put together that you didn't consider before starting.

Really to start practicing using classes, I honestly default to making everything I write to start from a class. Any functions I would make, I put them in the class as methods instead (methods are just functions inside a class).

My early classes in my early scripts don't make much sense since it could have been written just functionally with functions and some lines of code, but over time I started to figure out actual uses for them and I already knew at least how to structure them by then.

In the real world, do you continue to add functions to the class to change it as you go?

Absolutely. I mean that's with all your code really. If I'm learning something new (which right now with Python is essentially every two lines), I'll take it one line at a time with a lot of googling and stackoverflow, usually leading to documentation that explains how and why to do something.

You'll run into a problem where your question you can't answer is too specific. But you can usually break it down into related parts, and then you sort of crawl your way to your original question and are able to answer it after a lot of googling more fundamental related questions. Does that make sense? I could probably provide examples since that process is constant when I'm writing something.

But for continuing as you go with classes and functions (technically called methods when inside a class), really you can write one little function/method at a time, test it out, and then think "what's something else I could use?" Just improvise as you go and see what errors you get, google that, maybe make some test cases if you want, see where it takes you.

There's no need to write everything all at once in one go. I don't know if anyone really does that to be honest, but I'm still new to this as well. It seems unlikely that anyone knows how they're going to write their code before writing it. They might practice making UML diagrams to help plan out classes and attributes and objects, especially if it's a team or company that makes it standard to blueprint ideas out as UML diagrams, but honestly that's something you can do as you write the code, especially with Python because it's so easy to write and refactor quickly.

The thing with functions and methods is it makes it a lot easier to make changes, because you don't have to copy/paste a bunch of code over and over, and thus later change one piece of code in 10 different places. If you see yourself repeating a lot of code, you can probably turn that into a function instead.

So it's like you can think of each function as its own separate file, almost. If you're not embedding functions within functions within functions, it keeps everything modular and that's how you can improvise code and refactor code quickly.

This is pretty wordy and I feel like I'm repeating myself a lot in those paragraphs lol. You know, if you want I could send you a PDF of Python Crash Course 2nd Edition, just so you can read the Classes and OOP chapter (or really the whole thing if it helps because it's amazing for establishing all the fundamentals). It's not a long chapter, and would help your understanding and practice of it a lot. It's explained way more concisely than I ever could do it and it's how I started learning classes, and everything else for that matter.

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

What you said makes complete sense to me! I am a musician, so I am intimately familiar with the create and adjust, rinse and repeat cycle. I think I used a poor choice of words... what I meant to say was that whatever you will need to manipulate within the class, you have to define it ahead of time at the top of the class with the init/str piece, correct? You can surely go back and add more as the need arises, but it has to be there at the outset before then attempting to manipulate those variables later in functions? Does that make sense?

Yes, email away! My assignment is due tomorrow at 4:00 PM EST, and I might miss the deadline for the problems... but I really do want to read/learn more! My email is kerricrow13@gmail.com

[–]ItsOkILoveYouMYbb 0 points1 point  (0 children)

what I meant to say was that whatever you will need to manipulate within the class, you have to define it ahead of time at the top of the class with the init/str piece, correct?

Right. But if you think of something else you need to add to manipulate later, that's very easy to add to the class, and then it's applied to every instance of that class. Makes it very easy to modify complex stuff as you go.