Pure functions and Classes

If I understand this correctly our goal is to write pure functions and self-contained “Classes”. Classes still have to be able to call other Classes if necessary. That’s where I not sure of. How is that considered “self-contained”? Is it because the called class is “self-contained?

Concepts are the base of knowledge, must have good understanding of the concepts to put knowledge to work.

Thanks for you time and knowledge.

I suggest you go back to whatever you were reading and try to understand what that person meant. These terms are not necessarily well defined. But a couple notes:

A “pure function” is essentailly a functon with no side effects (more detail here: [Pure function - Wikipedia].

Pure functions are a good goal – they make code much easier to understand, test, and debug.

In a way, self-contained classes are quite different than pure functions – as a rule, an object-oriented approach used mutable classes with methods that work on the contained data – methods are functions, but a function that depends on the state of the class when called is not a pure function. You’ll find a lot of opinions about the benefits or lack thereof of an OO approach, but a lot of people find it useful.

A self contained class is one that holds all the state (data) it needs, and all the methods it needs to manipulate that data.

As to your point, classes don’t “call” other classes – they might call various functions, including methods of other classes, but that’s not quite the same thing [*]. But what i suspect you are getting at is that it is considered a good practice for the users of a class to only need to know about that class. This is sometimes called the Law of Demeter; [Demystifying the Law of Demeter principle | InfoWorld.

And that is a good principle to (try to) stick to.

I hope that helped a bit …

[*] Python has the feature of “callable” 'classes – but that just muddies the waters …

Thanks for clarifing the difference. I don’t want to learn bad habbits because I didn’t have an understanding of the goals of programing. Nor do I want to waste time doing something that others have proved is a poor way to program. Even if it got the job done, it would still be wrong and a poorly written program. Very hard to upgrade and maintain. What I would call a dirty program or script.
I’m reading on functional programing next. Trying to get a good handle on the concepts. It takes a bit to"own" Python programing and I’m just learning, but that is the goal.

It sounds like you ar thinking about this all the right way.

It’s a bit tricky – there’s a bit of the chicken-egg problem here – it’s really hard to get these concepts if you don’t yet know the implementation details, but if you hack away at the implementation, you can end up with a mess :frowning:

One thing to do is start with small problems (such as show up in various online courses / challenges), but once you have it working, take a moment to look at the solution with some of these larger concepts in mind – “does this look like good clean code?” – that’s when you can apply some of these ideas.

One resource I found very helpful is “The Pragmatic Programmer” [The Pragmatic Programmer, 20th Anniversary Edition: your journey to mastery by David Thomas, Andrew Hunt] – as the title suggests, it takes a pragmatic approach, rather than a philosophical one (e.g. “functional programming”, “OO design”.

Good luck!