Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / NewStats: 3,199,451 members, 7,971,626 topics. Date: Thursday, 10 October 2024 at 01:04 PM |
Nairaland Forum / Science/Technology / Programming / Pythonists Please Help!!....def Functionname(self)...confusing Me.. (1290 Views)
Help! My Industrial Training Is Confusing Me (2) (3) (4)
Pythonists Please Help!!....def Functionname(self)...confusing Me.. by phililp(m): 10:25am On Apr 27, 2017 |
dear programmers.. am among those that cudnt make it to Andela in the previous application so getting set for the next. so.. am really confused about the self. parameter in python... here is ma code class Customers: account_balance = 0.0 def _init_(self): self.account_type = str(input('account type:')) self.name = str(input('enter name of customer:')) self.account_balance = 0 def withdrawal(self): self.amount = int(input('specify amount you want withdrawal:')) if amount > self.account_balance: print('sorry; insufficient balance') else: self.account_balance -= amount return self.account_balance def deposit(self): deposit_amount = int(input('please enter deposit amount:')) self.account_balance += deposit_amount print('you just deposited ', deposit_amount, ' and you new balance is' , self.account_balance) customer1 = Customers() customer2 = Customers() the problem is about self.. now i created a class Customers and a class variable called account_balance i know that if i want to make use of the class variable in any of the modules in class Customer i wud simply do: self.account_balance my question is this: if i want to create a variable in a module; do i still need to tag it with the self.? like i did in the def widrawal(self) def withdrawal(self) self.amount = ........ or shud i just define the variable without the self? would appreciate a ditailed answer.. thanks |
Re: Pythonists Please Help!!....def Functionname(self)...confusing Me.. by nakaofficial(m): 8:21pm On Apr 29, 2017 |
You don't need to use self in module, you can simply use the variable amount instead. Below is a sample code: def withdrawal(amount): if amount > account_balance: print('sorry; insufficient balance') else: account_balance -= amount return account_balance def deposit(amount): account_balance += amount print('you just deposited, ' amount ' and you new balance is' , account_balance) return account_balance NB: saving the above code as bank.py >>> from bank import withdrawal, deposit account_balance = 5000 x = int(input('please enter deposit amount:')) >>> please enter deposit amount: 3000 withdrawal (x) >>> 2000 OR deposit (x) >>> 8000 Hope this helps. |
Re: Pythonists Please Help!!....def Functionname(self)...confusing Me.. by phililp(m): 8:42pm On Apr 29, 2017 |
thanks bro.. alot |
Re: Pythonists Please Help!!....def Functionname(self)...confusing Me.. by TechRex(m): 1:25am On Apr 30, 2017 |
the self variable is a constant variable taht should be in any method parameter declaration,.. The self variable makes it easier to access the class/program variables e.g socket = Socket.socket() request = "" def processRequest(self, request): self.request = request It is very important to add the self variable in your method parameters, some programmers might say it is not important, but in the actual sense it is **** important When you start coding with bigger erp frameworks like odoo 10, you willl really need to develop the norm of using self in you method...... 1 Like |
Re: Pythonists Please Help!!....def Functionname(self)...confusing Me.. by phililp(m): 11:00am On Apr 30, 2017 |
TechRex: what if u wana create a variable in the method.. that dont need to be supplied argument.. like say: def addition(self; num1; num2) result = self.num1 + self.num2 return result now is the result supposed to be self.result?? |
Re: Pythonists Please Help!!....def Functionname(self)...confusing Me.. by fleshbone(m): 1:31pm On Apr 30, 2017 |
In my opinion, the class being a class object, the def is ur method. We are suppose to initialize, therefore, u can use the built-in-function, __init__() constructor. The first arguement, self, is referring to the class name, and other arguement can follow. But where would the arguement land? Therefore, one need to create variables to hold this arguement when they're passed. To do this, Self.the_name_you_choose = one_ of_the_argument Self.the_name_you_choose = another_argument. After creating an instance (which is passing values into that class created) , one can then access this variables by 'classname.variable_name_in_the_class' Example Class point: #classname Def __init__(self, x, y): #initialising with a constructor Self.x = x #assigning classname.x Self.y = y #assigning classname.y Pt = point(5,6) #creating an object of classname Sum = int (pt.x + pt.y) #using the created object Print(sum) #print result I hope this help. I stand to be corrected as I started learning too less than a month now. phililp |
Re: Pythonists Please Help!!....def Functionname(self)...confusing Me.. by phililp(m): 3:01pm On Apr 30, 2017 |
fleshbone: thanks for ur contribution fleshbone... but i think nobody has gotten my point lemme make it clear if i want to create an instance variable that is not a parameter. and no argument is supposed to be passed to it.. do i stiill need to tag it with self?? example class customers: -----def add_two_nums(self; num1; num2) -------outcome = self.num1 + self.num2 -------return outcome [center]or[/center] class customers: -----def add_two_nums(self; num1; num2) -------self.outcome = self.num1 + self.num2 -------return self.outcome |
Re: Pythonists Please Help!!....def Functionname(self)...confusing Me.. by badthinds: 6:38pm On Apr 30, 2017 |
isorite guys...I guys the quuestion here has been answered. NOTE: Codes may perform identical functions, but with entirely different lines!. ...and no way is the best, the best way in our world is the most secure way! |
Re: Pythonists Please Help!!....def Functionname(self)...confusing Me.. by fleshbone(m): 6:51pm On Apr 30, 2017 |
phililp: Here, I see the num1 and num2 as ur argument. 'Self.outcome = num1 + num2' I don't think u need to put self before them. I would try it too sha. |
Re: Pythonists Please Help!!....def Functionname(self)...confusing Me.. by fleshbone(m): 7:15pm On Apr 30, 2017 |
Try this -------------------------------------------------------------- Class sum_it: --- Def __init__(self, num1, num2): ------- Self.answer = num1 + num2 Def main(): ----Ans = sum_it(5,10) --------Print(answer.ans) Main() ------------------------------------------------------------------------ The above worked for me. I just tried it. Goodluck phililp. |
Re: Pythonists Please Help!!....def Functionname(self)...confusing Me.. by TechRex(m): 10:11pm On Apr 30, 2017 |
phililp: Dont get you sir |
Re: Pythonists Please Help!!....def Functionname(self)...confusing Me.. by phililp(m): 11:12pm On Apr 30, 2017 |
Re: Pythonists Please Help!!....def Functionname(self)...confusing Me.. by phililp(m): 11:17pm On Apr 30, 2017 |
hey fleshbone.. looks like we both in same route.. i just started learning python a couple of weeks ago. u can add me up on fb lets share ideas.. username = "philips chike" linkedin: https://www.linkedin.com/in/philip-chike-54780313a/ thanks |
(1) (Reply)
Urgent Help Needed / Sr. Programmers For US Based Companies / Maiden Edition Of The Nigerian Universities Programming Contest
(Go Up)
Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health religion celebs tv-movies music-radio literature webmasters programming techmarket Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 28 |