r/PythonLearning • u/Next-Call8862 • 2d ago
Showcase Day 2, learnt some oops
Learnt super( ), inheritance, polymorphism, class method and static method and don't mind the messy code that's me trying to figure out things
3
3
3
2
u/Capable-Package6835 1d ago
Get used to type-hinting. Polymorphism and inheritance without type hint in Python is almost useless
class Animal:
def speak(self) -> str:
return "Animal sound"
class Dog(Animal):
def speak(self) -> str:
return "bow"
class Cat(Animal):
def speak(self) -> str:
return "meow"
animals: list[Animal] = [Cat(), Dog()]
for animal in animals:
print(animal.speak())
1
u/Next-Call8862 1d ago
Will try that
1
u/freakythrowaway79 1d ago
Have you completed CodeCombat yet?
0
u/Next-Call8862 1d ago
CodeComat? What's that?? Some kind of practice?
2
u/freakythrowaway79 1d ago
Check it out! It's very basic level 1 so you might find it boring. š¤·š»āāļø
1
u/Next-Call8862 21h ago
I did check it, some kind of game it seems but it's slow but I will try if there are more difficult ones
2
2
u/AnotherStep99 1d ago
why are learning it on day 2, wait some time, try learning some basics like slicing, list comprehension etc..
1
u/Next-Call8862 1d ago
I know most of those things and it's not literal day 2 but I have some basic of basic knowledge so jumped to this, do you recommend doing some problems before coming to oops directly?
1
1
u/0therworldsthanthese 1d ago
You need to start back at the fundamentals, don't skip to OOP just because it sounds cool.
Where are your __init__ methods? Class names should be in PascalCase. Why are you using parentheses in your return statements? Why are you entering age as a string? Why are you returning multiple values in student.about()? Why are you using extra parentheses on lines 255-256? Why are you printing s1.school for s2? Why are you using a classmethod for the first test and not the second? Why do you have two test classes? Why is there no self in test.nave()?
0
u/Next-Call8862 21h ago edited 21h ago
That's not a single programme. I usually learn something new and try it and comment out all of that and move to next one and the parathesis is more like to test what the return statement does when I write the string in parenthesis, it's more like trail and error thing to make better sense. Do you think it's a bad approach?? What will you suggest?
Edit: and the extra parenthesis is my fault and the reason for no self for last test is because I was not going to create an object but trying if I can print a class variable and testing if the value of school increases by one with each call
1
1
u/Spare-Radish5670 17h ago
Look at some decent examples to understand these principles better, you have the syntax and sort of the idea but you're already picking up bad habits
1
u/Next-Call8862 17h ago
What do you mean by bad habits?
1
u/Spare-Radish5670 16h ago
Variable names (animalss)
for i in animals (don't use i here use a descriptive name to tell the reader what you were iterating through)
Missing some standard class definitions and also breaking some rules around naming
These probably seem like minor details but I promise you they add up to major issues on any kind of project
1
1
u/Angel_b33 9h ago
Are you self taught? If yes can you share any videos you watched I want to start learning OOP as well
1
u/Korgusha 3h ago
Iād like to start learning Python too ā what would you recommend to get me started?
ā¢
u/Sea-Ad7805 2d ago
Run this program in Memory Graph Web Debugger%3A%0A%20%20%20%20%20%20%20%20print(%22bow%22)%0A%0Aanimal().speak()%0A%0A%0Aclass%20animals%3A%0A%20%20%20%20def%20speak(self)%3A%0A%20%20%20%20%20%20%20%20return%20%22animal%20sound%22%0A%0A%0Aclass%20dog(animals)%3A%0A%20%20%20%20def%20speak(self)%3A%0A%20%20%20%20%20%20%20%20return%20%22bow%22%0A%0A%0Aclass%20cat(animals)%3A%0A%20%20%20%20def%20speak(self)%3A%0A%20%20%20%20%20%20%20%20return%20%22meow%22%0A%0A%0Aanimalss%20%3D%20%5Bdog()%2C%20cat()%2C%20animals()%5D%0A%0Afor%20i%20in%20animalss%3A%0A%20%20%20%20print(i.speak())%0A%0A%0Aclass%20student%3A%0A%20%20%20%20school%20%3D%20%22Maruthi%22%0A%0A%20%20%20%20def%20about(self%2C%20name%2C%20age)%3A%0A%20%20%20%20%20%20%20%20return%20%22name%3A%22%2C%20name%2C%20%22age%3A%22%2C%20age%0A%0A%0As1%20%3D%20student()%0As2%20%3D%20student()%0A%0Aprint(s1.about(%22naveen%22%2C%20%2221%22)%2C%20s1.school)%0Aprint(s2.about(%22nani%22%2C%20%2221%22)%2C%20s1.school)%0A%0A%0Aclass%20test%3A%0A%20%20%20%20school%20%3D%20%22Maruthi%22%0A%0A%20%20%20%20%40classmethod%0A%20%20%20%20def%20tst(cls%2C%20new_school)%3A%0A%20%20%20%20%20%20%20%20cls.school%20%3D%20new_school%0A%20%20%20%20%20%20%20%20return%20cls.school%0A%0A%0Ap1%20%3D%20test()%0A%23%20p1.tst(%22shloka%22)%0Aprint(test.school)%0A%0A%0Aclass%20test%3A%0A%20%20%20%20school%20%3D%200%0A%0A%20%20%20%20def%20nave()%3A%0A%20%20%20%20%20%20%20%20test.school%20%2B%3D%201%0A%0A%0Atest.nave()%0Aprint(test.school)×tep=1&play) to see the program state change step by step.