All Questions
33 questions
36
votes
3
answers
39k
views
Is it ok to have multiple classes in the same file in Python?
In Java and PHP (although not strictly required), you are expected to write each class on its own file, with file's name is that of the class as a best practice.
But in Python, or at least in the ...
29
votes
2
answers
73k
views
Classes vs. modules in Python
Python has many modules (such as re) that perform a specific set of actions. You can call the functions of this module and get results, and the module as a whole has an idea behind it (in this case, ...
22
votes
1
answer
52k
views
Why use classes when programming a tkinter gui in python
I program primarily in python and have programmed a couple of GUI's with Tkinter, every tutorial I have ever seen has recommended defining and using a class for the GUI, but my GUI runs flawlessly ...
11
votes
1
answer
12k
views
When to use private methods in Python
I have a class, but every method in it should be private (apart form __init__ and __str__). Should I denote every method with a double underscore, or is that deemed bad practice?
9
votes
2
answers
6k
views
Should I split a Python class with many methods into multiple classes?
I have a class that will end up having more than ~30 methods. They all make sense to be part of the same class because they require access to the same data.
However, does it make any sense to split ...
8
votes
5
answers
2k
views
Python: What is the point of using "import"?
I am not very clear on this aspect. Let's say you have a bunch of .py files that are their own separate modules.
Why does each .py file need to import the others when they use that class? Or do they? ...
7
votes
1
answer
20k
views
Is calling the superclass constructor in a subclass really important?
The following piece of Python code uses a superclass solely as the repository of functions that one of more subclasses may draw from:
class Class(object):
''' A trivial repository for functions ...
7
votes
2
answers
3k
views
In Python, is there any difference (apart from a few listed exceptions) between classes and dictionaries?
My logic goes like this:
def A(val):
return {'a':val}
print(A(7)['a'])
is the same as
class A:
def __init__(self, val):
self.a = val
print(A(7).a)
Obviously, there are problems and ...
4
votes
1
answer
1k
views
Avoiding tightly coupled class definitions in Python for has-a relationships
I have the following code:
class Car(object):
def __init__(self, my_id):
self.my_id = my_id
self.color = color
self.brand = brand
self.get_color()
self....
3
votes
2
answers
1k
views
Is “A programmer-defined type.” a right definition of "class" in Python?
In Think Python 2e "class" is defined as "A programmer-defined type. A class definition creates a new class object."
But isn't a built-in type considered a class too?
Using Python 3.4.0, the ...
3
votes
2
answers
464
views
Accessing properties from embedded objects as attributes of container class
In Python, I have a class C which embeds objects from classes A and B. Is it considered good practice to creates shortcuts from the properties of embedded objects of classes A and B as attributes of ...
3
votes
3
answers
7k
views
What is the difference between proxy class and delegation in Python?
Wiki:
A proxy, in its most general form, is a class functioning as an interface to something else. The proxy could interface to anything: a network connection, a large object in memory, a file, or ...
3
votes
1
answer
78
views
Should I be taking these as arguments or define them in a sub class?
So I am making a mario clone in pygame and I have a base class Character and two sub classes, Mario and Luigi. The methods that the Character class defines require a significant amount of attributes ...
3
votes
1
answer
5k
views
Using class like an object in Python
I am learning from Learn Python the hard way where I have come across a study drill where they want to know that whether a class can be used like an object.
As I have experimented:
class A(object):
...
2
votes
2
answers
1k
views
python - differences between reusable code vs. code for solving specific tasks
Reusable code (ex. libraries and frameworks) and code written to solve a specific task and not meant to be reused as a general tool (for example, code being used only by my 6 person team in a private ...