Return ''.format(self.name, self.balance)Įxample usage being: customer1 = BankAccount('Alex')Īs regard to the "one class per file" rule, it may make sense in languages where a file does not bear much meaning.īut in python a file is a module. read-write) with the simplicity of attributes access.Īppart from that, I would add either _str_ or _repr_ (or both) as methods in your class to simplify printing accounts: class BankAccount: There is no mechanism in place to restrict their access, so user that know what they are doing can still use them but if anything goes wrong, it will most likely be their fault, not yours.Īlso, in the context of versionned libraries, internals are not subject to backward compatibility and can change without notice on a new release.īut instead of methods as getter or setters, we prefer properties that allows the same kind of flexibility (read-only vs. Python doesn't have private anything but we still have a need to separate inners of a class from the public interface.īy convention, attributes or methods whose name starts with a single underscore are considered internals of the class and, thus, not part of the public API. Is it still good to do this when the functions are short and simple, like withdraw(x) in a bank account?ĭef get_balance(self): #are accessor methods needed in Python? My IDE suggest I put a comment beneath each function signature.Python doesn't have private, so is there a point to having accessor methods like I do below?. In Python you can have multiple functions and classes in a single file (module), but is it considered bad practice to do so?.Since I'm self teaching, I'd appreciate any feedback and constructive criticism. I am learning Python and in particular learning about classes.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |