r/PythonLearning • • 17d ago

pro at OOPs thanks to u/clcodingupdates... ty bro...

Post image

i stayed up all night making this. python day 3 complete.

46 Upvotes

26 comments sorted by

•

u/Sea-Ad7805 17d ago

Run program above by clicking Memory Graph Web Debugger and see its state change step by step.

OOP (Object Oriented Programming) usually involves writing classes using the class keyword, this only uses functions not classes. It's nice, but better not call it OOP.

→ More replies (1)

6

u/Mooball123 17d ago

It’s solid code but not really OOP. For OOP you define your own class (or object) and write the code to manipulate your class. Something like

class Animal:

Def __init__(self, animal: str, name: str = “”):

Self.animal = animal
self.name = name

A couple of things here. When you write a new class you write the __init__ method. This runs whenever you make a new instance of that class. This always takes the argument self, which just means its own instance of the object.

Then the “: str” is type hinting, just to help keep your code clean and organised. The “= “”” means that it’s an optional argument, if you don’t give a name argument, then it defaults to “”.

You can then make a method on the class

def greet(self):

If self.name == “”:
print(f”{self.animal} says hi”)
else:
print(f”{self.name} the {self.animal} says hi”)

Another couple of things. name and animal are attributes of the instance of animal, and you access them using self.name/animal. Now that you have a method and a class, you can do this

badger = Animal(“Badger”, “Barry)
badger.greet()

Will print

Barry the Badger says hi

2

u/Achereto 13d ago

It’s solid code but not really OOP.

I thought that was the point of the joke.

-4

u/saul_soprano 17d ago

clcodingupdates told me it was oops

3

u/Mooball123 17d ago

I mean, it’s more of a precursor to object oriented programming. You’re using good practices, variables have meaningful names, you’re using functions for repetitive code.

You’re doing all the ground work of OOP without actually using OOP. Try out writing the same program but make an animal class and some methods, and you’ll start to see that it’s way more scalable and intuitive then functions and lists alone

1

u/olaf33_4410144 16d ago edited 16d ago

Maybe you should look at the definition of oop yourself.

Usually it involves creating classes/objects that encapsulate both data and functions. Some other notable features include polymorphism and inheritance (though nowadays many people discourage the heavy use of inheritance).

Your code doesn't do any of these. Still good practice using functions and arrays.

I think this article does a decent job of explaining more: https://realpython.com/python3-object-oriented-programming/

1

u/Dancing-umbra 16d ago

Why is inheritance now not best practise?

1

u/olaf33_4410144 16d ago

It's not always bad, but both multi level inheritance and inheriting from multiple base classes tends to make things more difficult. For example changing a base class many others (indirectly) inherit from very quickly breaks things you weren't even aware of. It can also produce a tighter coupling between classes that makes reusing and refactoring classes more difficult.

Nowadays many people prefer composition, protocols and interfaces instead of inheritance. For example: https://typing.python.org/en/latest/spec/protocol.html

1

u/Ender_Locke 17d ago

it’s neat code but it’s not really oop you need to create an animal class and use that. you’re just printing strings of animals and their names, kind and name should be attribute (variables) in your class you set

animal1 = Animal(kind=“cat”, name=“ren”)

print(animal1.name)

-4

u/saul_soprano 17d ago

clcodingupdates told me it was oops

2

u/Ender_Locke 17d ago

all good you’ve got the concepts down now go make a class !

1

u/Orgasml 17d ago

And everyone else is telling you it's not (because it's not). Also, it's OOP (Object-Oriented Programming), not oops. In order to have objects you need classes and usually instances of those classes.

2

u/sleepbot63 16d ago

This isn't really Object Oriented Programming.

Also I'm sorry for the code you are about to see:

S="Ricky,Ishnu,Trutu,Cupta Z,Steven,Lenny,Badger,Swine,Rose Bush,Cow (Worshipped),Michael the Salamander".split(",") print(*[f"New friend named {S[hash(k)%6]}, a {k}"for k in S[6:]],"Friends saying namaste:",*[f' {k} "{S[hash(k)%6]}" says namaste!'for k in S[6:]],"All 5 said namaste.",sep="\n"

this does exactly what you do but is a golfed version of it

1

u/DistearRoyl 17d ago

A somewhat advanced technique that involves using the `random.Random` class indirectly via a module.

1

u/mattynmax 17d ago

This isn’t OOP but ok.

-3

u/ZookeepergameFew6406 17d ago

Dont get into OOP. I can link you a 9min video why its not good. And i can explain why its nit good

1

u/p1geondove 17d ago

yes please, why is oop not good?

4

u/JaleyHoelOsment 16d ago

because they watched a single 9 minute video that told them it’s bad

-2

u/ZookeepergameFew6406 16d ago

TLDR: you’re shuffling papers around for days yet you haven’t solved a problem. You invented a second problem and tried solving that.

1

u/olaf33_4410144 16d ago

It's not all bad, but certain parts like heavy use of inheritance (especially multi level inheritance) can make code more difficult to reason about and aren't as popular anymore.

1

u/ZookeepergameFew6406 16d ago

The videos:

Short:
https://youtu.be/04ksL1hf_p8?is=2-XrWEug1Mk7ro0B

Long:
https://youtu.be/QM1iUe6IofM?is=IOi_-MUF9TYprQl_

https://youtu.be/wo84LFzx5nI?is=2yYZ42HAmIkCRfm3

Tldr: all you need is encapsulation. The longer videos are great but might be difficult to understand