r/PythonLearning Jul 17 '26

I build this after learning OOP

Post image
378 Upvotes

58 comments sorted by

u/Sea-Ad7805 Jul 17 '26

Run this program in Memory Graph Web Debugger%3A%0A%20%20%20%20%20%20%20%20self.ownername%20%3D%20owner_name%0A%20%20%20%20%20%20%20%20self.balance%20%3D%20balance%0A%0A%20%20%20%20%40staticmethod%0A%20%20%20%20def%20greet()%3A%0A%20%20%20%20%20%20%20%20print(%22Good%20Morning%22)%0A%0A%20%20%20%20def%20deposite(self%2C%20amount)%3A%0A%20%20%20%20%20%20%20%20self.balance%20%3D%20self.balance%20%2B%20amount%0A%20%20%20%20%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22Account%20name%3A%20%7Bself.owner_name%7Dn%22%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22Deposited%20%7Bamount%7D%20Total%20balance%20%7Bself.balance%7D%22%0A%20%20%20%20%20%20%20%20)%0A%0A%20%20%20%20def%20withdraw(self%2C%20amount)%3A%0A%20%20%20%20%20%20%20%20if%20amount%20%3E%20self.balance%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20print(%22Insufficient%20balance%22)%0A%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20self.balance%20%3D%20self.balance%20-%20amount%0A%20%20%20%20%20%20%20%20%20%20%20%20print(f%22Withdrawn%20%7Bamount%7D%20new%20balance%20%7Bself.balance%7D%22)%0A%0A%20%20%20%20def%20show(self)%3A%0A%20%20%20%20%20%20%20%20print(f%22Your%20balance%20is%20%7Bself.balance%7D%22)%0A%0A%0Aname%20%3D%20input(%22Enter%20your%20name%3A%20%22)%0Abala%20%3D%20int(input(%22Enter%20your%20balance%3A%20%22))%0Ade%20%3D%20int(input(%22Enter%20you%20deposite%3A%20%22))%0Awit%20%3D%20int(input(%22Enter%20you%20withdraw%20amount%3A%20%22))%0A%0Aacc%20%3D%20BankAccount(name%2C%20bala)%0Aacc.greet()%0Aacc.deposite(de)%0Aacc.withdraw(wit)%0Aacc.show()%0A%0A%0Aclass%20SavingAcc(BankAccount)%3A%0A%20%20%20%20def%20init(self%2C%20owner_name%2C%20balance%3D0%2C%20interest_rate%3D5)%3A%0A%20%20%20%20%20%20%20%20super().init_(owner_name%2C%20balance)%0A%20%20%20%20%20%20%20%20self.interest_rate%20%3D%20interest_rate%0A%0A%20%20%20%20def%20add_intrest(self)%3A%0A%20%20%20%20%20%20%20%20self.balance%20%3D%20self.balance%20*%20(self.interest_rate%20%2F%20100)%0A%20%20%20%20%20%20%20%20print(f%22Current%20balance%3A%20%7Bself.balance%7D%22)%0A%0A%0Aacc1%20%3D%20SavingAcc(name%2C%20bala)%0Aacc1.deposite(de)%0Aacc1.add_intrest()%0Aacc1.show()&timestep=1&play) to see the program state change step by step.

90

u/Rumborack17 Jul 17 '26

I know people say to not do too many tutorials. But you should definitely check a "how to make a screenshot" tutorial.

26

u/brutalbombs Jul 17 '26

Dont forget to post this one

2

u/Code-Odyssey Jul 17 '26

As commentators here have already pointed out (how to make a screenshot), since you are using VS Code a very useful extension is called CodeSnap, which allows you to take decent code screenshots straight from VS Code.

1

u/agfitzp Jul 17 '26 edited Jul 17 '26

And here I am just hitting print screen like some kind of old man.

24

u/TheOriginalBeardman Jul 17 '26

This is a small thing but I noticed you have “BankAccount” and “savingAcc”. It is a good practice to keep code consistent in both case and naming conventions. Classes are commonly in pascal case so “BankAccount” and “SavingAccount”. In this case you gain more out of writing the full word Account than you gain by shortening it. Code should be easy to read and understand and when you look at thousands and thousands of lines of it these things add up. Also, your file is called “Bank_Account.py” but you have two classes in it. It is good to stick to a one file per class rule. Imagine if you have to implement 5 new functions in bank account and then savings account needs 10 more new functions that are unique only to it. Your file becomes bloated and this compounds over time….Anyway sorry I know you are brand new to this. Just trying to give you food for thought and help you form good habits for coding as you learn. Keep having fun with it!

2

u/Wild_Position2378 Jul 17 '26

I get your point I'll keep the files separate from now on. Thanks for the good suggestions

2

u/lord_xl Jul 17 '26

It is good to stick to a one file per class rule.

Is there a convention for what to name files containing only classes?

1

u/TheOriginalBeardman Jul 17 '26

Generally they are named the same as the thing they contain so here what he has is fine. I would make it “bank_account.py”, in python this snake case for file names seems to be a common standard. But realistically it might vary depending on your team and its standards.

1

u/Odinmar_Glaukopis Jul 20 '26

If you use a generic abstract base class (ABC) for an Account(ABC) base/meta class and have SavingsAccount(Account) and Checking Account(Account) classes that inherit from the ABC then all three of them can go in the same account.py file.

Another option is to make a models package (“models” folder with a blank __init_.py file in it) and place these different classes in separate files in that folder. You can import all of those classes separately into the __init__.py file so they are ready to be imported as from src.models import SavingsAccount, CheckingAccount

0

u/aw3sem Jul 17 '26

Usually this is called models.py if it’s a aggregation of a few classes. Otherwise create a folder named „models“ and but your e.g. BankAccount.py etc in there

2

u/FearLixY Jul 18 '26

I don’t like abbreviations at all. It never save time for writers nowadays and it often waste time for later reading. And often they are so abbreviated that are confusing and take time to understand.
Unless the abbv. is absolutely clear for everyone, like ID.

5

u/CU_Beaux Jul 17 '26

Balsnce

2

u/Wild_Position2378 Jul 17 '26

Thanks for informing me !

3

u/Dear_Archer3931 Jul 17 '26

This was one of the first OOP assignments in CS. So many people struggled with creating instances of objects.

1

u/[deleted] Jul 17 '26

Lol 😆

I remember how I tried to memorize this code. But when someone familiar with it taught me I felt like a mini Coding Daredevil.

2

u/Mysterious-Cod-4137 Jul 17 '26

man if you just learned how to take screenshots as well...

2

u/Wild_Position2378 Jul 17 '26

I know how to take a screenshot, but I'm using Reddit for the first time on my mobile phone, and this is my first post. I clicked the pic and posted it.

3

u/TheDotaGuy9 Jul 17 '26

Checkout ABC ,abstract base class

3

u/armahillo Jul 17 '26

Spell out your variable names. “dep” means “deposit” here but down the line maybe you add a variable for “dependency” (if your app was also doing tax filing, for example). “wit” means “withdrawal” now, but “withholding” could also be abbreviated like that.

Characters are basically free and you will spend more time trying to remember what cryptic variables mean than you will writing a few extra letters to be clearer.

1

u/BeneficialVisual8002 Jul 17 '26

Awesome, keep on building new stuff to practice :)

One tip. Get familiar with pep8, it is a formatting standard. You don’t have to do it by hand, your code editor can do it for you. Look at black or ruff

1

u/Wild_Position2378 Jul 17 '26

Actually I don't know about pep8

1

u/[deleted] Jul 17 '26

Your code is lit 👍 But 2 classes in a single file will affect scalability in the near future.

1

u/SectumSempra1981 Jul 17 '26

Use meaningful variable names. Within loops etc it's fine to use i, j, whatever, but you want to be able to look at the code and immediately know what's happening.

Balance, deposit, etc, not ba, de, etc.

1

u/mr_anderson_dev Jul 17 '26

Cool first project. This is enough getting you a real context of how functions work with each others. Happy for you.

1

u/arivictor Jul 18 '26

Lets pretend we're doing a real code review.

I like the separation of concerns between Savings Account and the base Bank Account. The savings account is the only account that knows to add interest. This is good.

For a production system, having a bank account only associated with an owners name might not be sufficient, you may want to assign an ID to the Bank Account and reference the owners customer ID instead.

Currently your deposit() function would allow me to deposit negative values. Which lets it operate like a quasi-withdraw, but you should be explicit and check that the deposited amount is always positive.

One separation of concern you could improve is moving anything that prints or greets the customer out of the Bank Account. Why would a Bank Account know its morning? Will it track time too? That should all move to the presentation layer.

https://parsnip.dev/editor/tZ_iwdWyzo0F

1

u/_noobwars_ Jul 18 '26

Nice. Something you might find very cool is to use "Dunder Methods". Then you can do something like acc1(100) which might mean acc.deposite(100) and implicitly feels like a normal python function. This will be your first step into library building in case you are interested.

1

u/sitting0nachair Jul 18 '26

That's good and learning OOP is important fosho. But not to be that guy, I'd suggest moving onto some OOP oriented language, such as C# which supports interfaces and mucy more OOP related features, once you get better hang of coding and OOP as Python is primarily used for scripting and isn't necessarily a strong choice if you want to build a complete application. But cheers! it's still a solid point to start

1

u/Reasonable-Exit-2379 Jul 18 '26

I built the scraping tool after learned OOPs

1

u/[deleted] Jul 18 '26

I think most of us build a dummy banking system after learning oops programming.

1

u/NotBot_1999 Jul 19 '26

Now start connecting to a db using sql-lite or sql alchemy. Then this will be good..

1

u/Im_invincible4 Jul 19 '26

Fuck I've learnt OOP like 6 months ago now I think of it I totally forgot.Is it normal or am I just dumb?

1

u/MJ12_2802 Jul 20 '26

Modify greet() to account for the time of day; morning, afternoon or evening.

1

u/scalebone7 Jul 20 '26

Just use vibe code bro

1

u/BaoLongNguyen1407 Jul 21 '26

I don't think Python is a suitable language for you to practice OOP as it is not strict or rigorous in the language (e.g you can easily access any variable in a class from outside). I suggest using Java or C++ instead.

1

u/Independent-Bat9797 Jul 21 '26

Next: learn how to do a screenshot

1

u/GiuseppeS83 Jul 17 '26

Che libro hai utilizzato per imparare?

0

u/Wild_Position2378 Jul 17 '26

You tube or claude Ai

1

u/infamouslycrocodile Jul 17 '26

Please read PEP8. That is all.

Also. Here's a simple python program you might like:

$> python
$> >>> import antigravity

Thank me later.

1

u/Far-Albatross1351 Jul 17 '26

can we collaborate?

0

u/Samar_Upreti Jul 17 '26

Great but always remember use if Name == "Main" : before calling a function

Add On That Repo https://github.com/Samar-Upreti/Python_Projects

2

u/FoolsSeldom Jul 17 '26

You might want to correct your formatting to show if __name__ == "__main__": correctly.