Ni hung <niih...@gmail.com> Wrote in message: (Please post in text format, not html. It doesn't matter for your particular message, but several things can go wrong, where some or most of us do not see what you meant to post)
> I am learning programming using python. I think of solving a > problem using functions and for this reason all/most of my > code consists of functions and no classes. I have some understanding of > classes/Object Oriented Programming. I can write simple classes but I do not > understand when to use classes Welcome to the forum., and to Python. You may not realize it, but you're already using classes in even the simplest Python program. int is a class, list and dict are classes. Even a function is an instance of a class. So what you're really asking is when you should create your own new classes. The short answer is whenever the standard ones (there are many hundreds of them, maybe thousands) is inadequate for your needs. When writing simple programs, compositions of existing classes can take you a long way. When you need something a bit trickier, Python has ways of generating classes that you might not realize, such as namedtuple. At its simplest, a class is a way to bundle up data items and method items so that each instance has all of those attributes with a simple call to the constructor. The simplest class is probably NoneType, which has about 20 attributes, I think all dunder ones. (Use dir () to look at objects and find their attributes, and help () to learn more about most attributes) But sooner or later, you find yourself with a dict of keys and values, where the values are lists of tuples, and you'll throw up your hands and write a nice class to make it easier to keep track of things. -- DaveA _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor