r/PythonLearning 6d ago

I build this after learning OOP

Post image
365 Upvotes

57 comments sorted by

u/Sea-Ad7805 6d ago

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.

88

u/Rumborack17 6d ago

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

25

u/brutalbombs 6d ago

Dont forget to post this one

2

u/Code-Odyssey 5d ago

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 5d ago edited 5d ago

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

25

u/TheOriginalBeardman 6d ago

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 6d ago

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

2

u/lord_xl 6d ago

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 5d ago

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 2d ago

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 5d ago

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

1

u/TimeScallion6159 5d ago

great advice

2

u/FearLixY 5d ago

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.

1

u/Dismal-Tax-8503 3d ago

I have no mind idea what coding is, but will you teach me I jus believe I can grasp whatever it is and make sometime out of it

4

u/CU_Beaux 6d ago

Balsnce

1

u/Wild_Position2378 6d ago

Thanks for informing me !

1

u/Vizekoenig_Toss_It 5d ago

Good morninig

2

u/Dear_Archer3931 6d ago

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

1

u/[deleted] 5d ago

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 6d ago

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

1

u/Wild_Position2378 5d ago

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.

2

u/TheDotaGuy9 6d ago

Checkout ABC ,abstract base class

2

u/armahillo 5d ago

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.

3

u/TrieMond 6d ago

A small thing that might become annoying in larger codebases, when dealing with many different OOP objects: greet is the action of a person, not a bank account. Generally it is good practice to use OOp to represent real objects through their state (fields) and actions (functions). In this case greet should either be static (you just call the function normally without linking it to the BankAccount object) or should probably be part of some future Person class if the program needs it (like if you want people to be able to perform many actions which require keeping track of their state, for example if you want the greet function to output the name of the person too). Overall it is a good idea to keep yourself organized by thinking about which object should implement certain functionalities to mirror the real world problems you are trying to solve.

1

u/BeneficialVisual8002 6d ago

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 6d ago

Actually I don't know about pep8

1

u/[deleted] 5d ago

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

1

u/SectumSempra1981 5d ago

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 5d ago

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

1

u/arivictor 5d ago

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_ 5d ago

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 5d ago

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 5d ago

I built the scraping tool after learned OOPs

1

u/[deleted] 4d ago

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

1

u/NotBot_1999 4d ago

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

1

u/Im_invincible4 4d ago

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 3d ago

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

1

u/Sad_Kaleidoscope8412 3d ago

Looks cool. Try creating application out of it. Might be tkinter.

1

u/scalebone7 3d ago

Just use vibe code bro

1

u/BaoLongNguyen1407 2d ago

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/voids_dream 2d ago

it's great to seee, good job dude

1

u/Independent-Bat9797 1d ago

Next: learn how to do a screenshot

1

u/GiuseppeS83 6d ago

Che libro hai utilizzato per imparare?

0

u/Wild_Position2378 6d ago

You tube or claude Ai

1

u/infamouslycrocodile 6d ago

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 6d ago

can we collaborate?

0

u/Samar_Upreti 6d ago

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 6d ago

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

-1

u/OwnBusiness1727 6d ago

Nice Info