Skip to content
Advertisement

Tag: class

Importing modules inside python class

I’m currently writing a class that needs os, stat and some others. What’s the best way to import these modules in my class? I’m thinking about when others will use it, I want the ‘dependency’ modules to be already imported when the class is instantiated. Now I’m importing them in my methods, but maybe there’s a better solution. Answer If

What’s an example use case for a Python classmethod?

I’ve read What are Class methods in Python for? but the examples in that post are complex. I am looking for a clear, simple, bare-bones example of a particular use case for classmethods in Python. Can you name a small, specific example use case where a Python classmethod would be the right tool for the job? Answer Helper methods for

Python : terminology ‘class’ VS ‘type’

Just a simple question : when should I use the term ‘class’, and when should I use the term ‘type’ in Python ? is ‘class’ only for user-defined types, and ‘type’ for built-in types ? or now that everything is a type … should I use always ‘type’ even for user-defined classes ? … ? Answer It is more or

Print all variables in a class? – Python

I’m making a program that can access data stored inside a class. So for example I have this class: So using this I can call out a single variable like: But if I wanted to print all variables, I’m a little lost. If I run: I get: But I want to be able to print the actual data of those.

Printing all instances of a class

With a class in Python, how do I define a function to print every single instance of the class in a format defined in the function? Answer I see two options in this case: Garbage collector This has the disadvantage of being very slow when you have a lot of objects, but works with types over which you have no

Advertisement