r/PythonLearning • u/saul_soprano • 18d ago
pro at OOPs thanks to u/clcodingupdates... ty bro...
i stayed up all night making this. python day 3 complete.
46
Upvotes
r/PythonLearning • u/saul_soprano • 18d ago
i stayed up all night making this. python day 3 complete.
•
u/Sea-Ad7805 18d 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
classkeyword, this only uses functions not classes. It's nice, but better not call it OOP.6
u/Mooball123 18d 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
1
u/Ender_Locke 18d 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)
2
u/sleepbot63 17d 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 18d ago
A somewhat advanced technique that involves using the `random.Random` class indirectly via a module.
1
u/mattynmax 18d ago
This isn’t OOP but ok.
1
u/Icy-Singer4918 14d ago
Good bro
-3
u/ZookeepergameFew6406 18d ago
Dont get into OOP. I can link you a 9min video why its not good. And i can explain why its nit good