Python Class __init__ Confirmation -


im trying understand python classes. little confused around defining __init__. if have 4 functions created taking various input variables. have assign each variable in __init__?

 class thing:      def __init__(self, arguments, name, address, phone_number, other):          self.arguments = arguments          self.name = name          self.address = address          self.phone_number = phone_number          self.other = other       def first(self, name):          print self.name       def arguments(self, arguments):          print self.arguments       def address(self, address, phone_number):          print self.address + str(self.phone_number)       def other(self, other):          print self.other 

the above made please excuse pointlessness (arguably point illustrate question guess not pointless).

no doubt im going loads of down marks question reason have been reading various books (learning python hard way, python beginners) , been reading various tutorials online none of them confirm "you must add every variable in init function". understanding __init__ little better appreciated.

firstly: __init__() function special in called while creating new instance of class. apart that, function other function, can call manually if want.

then: free want parameters passed function. often, parameters passed __init__() stored inside object (self) being initialized. sometimes, used in other ways , result stored in self, passing hostname , using create socket - store socket. don't have parameters though, can ignore them in first() function, receives parameter name ignored. note parameter name , similar attribute self.name different things!

notes:

  • it uncommon ignore parameters passed __init__() though, uncommon ignore parameters in general. in cases, "reserved" can used derived classes typically unintended (as first() function, guess).
  • check out pep8, style guide. adhering make easier others read code. e.g. first() should first() instead.
  • upgrade python 3. don't think there's excuse nowadays learn python 2 , littley excuse except maintenance use in general.

Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -