Documenting ctypes fields [closed] - python

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
I am working on a python module that is a convenience wrapper for a c library. A python Simulation class is simply a ctypes structure with a few additional helper functions. Most parameters in Simulation can just be set using the _fields_ variable. I'm wondering how to document these properly. Should I just add it to the Simulation docstring? Or should I write getter/setter methods just so I can document the variables?

When I do similar things, if it is a small class I will put everything in the same class, but if it is bigger, I typically make a class that only contains the fields, and then a subclass of that with functions. Then you can have a docstring for your fields class and a separate docstring for your simulation functions.
YMMV, but I would never consider adding getters and setters for the sole purpose of making the documentation conform to some real or imaginary ideal.

Related

Should utility functions used within a class method be part of the class or outside of the class in its own file or same file as the class? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 days ago.
Improve this question
Suppose you have a class in test.py
class test:
def method1(self, arg1, arg2):
# do some stuff and call utils_func(arg1, arg2)
utils_func is only used within method1. I am wondering which one of the following is more appropriate:
Define utils_func as a method for test. I'm not sure if this makes much sense since it doesn't directly operate on any attribute of method1.
Don't do #1, but define utils_func within test.py so that you don't have to import it.
#3) Do #2, but define utils_func in a separate file, e.g., utils.py and then import it in test.py.
I think this question is partly opinion-based, but I'm wondering if there's any standard way to do this for Python development, and what things I should consider when making a decision?

Is it Pythonic to have global functions not part of a class? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
In my test suite I have a file called "utils.py" in which I have assorted functions required by many of the tests. To accomplish this I created a "Utils" class and had all of the functions inside it.
A colleague, with more Python experience, insisted that there should be no such class and instead all of these functions should be top-level. Thus "Utils.get_feature_id()" became "get_feature_id()".
Would you concur with his assertion?
Robert
In this case, the only thing you are using the class for is to essentially have the "methods" within a namespace. I agree with your coworker, you could have these functions be top level, but kept in a module so they indeed get put in a namespace, e.g.
import utils
utils.get_feature_id()
Instead of the following, which would presume you marked all your methods as #staticmethod which again just adds an unnecessary layer.
import utils
utils.Utils.get_feature_id()
Yes. If a function doesn't access a self, it should most likely not be a method. You can use a full module if your goal was to arrange your functions in a distinct namespace. Python uses namespaces everywhere, so we need not shy away from global names like C++ tends to and Java enforces (effectively, because they're not that global after all).

Is it a bad practice in Python to avoid implementing getter functions? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
I am working on a Python project and since the instance variable names can be directly accessed by specifying class.variablename, I was wondering if it is recommended to implement getter functions for the same. I have not declared the variables as private.
In languages like Java, you implement getters and setters so that you do not need to change the class's interface if you want to add additional processing (such as value validation, access control, or logging) when getting or setting an attribute. This is particularly important if your classes will be used by applications you didn't write.
In Python, you can add code to attributes with #property without changing the interface, so you should do that instead. Use regular attribute access to start, and add #property later if you need to add a behavior.
Even then, keep your getter and setter functions simple. Attribute access is expected to be reasonably quick and you should avoid violating that expectation. If you need to do significant work to obtain a value, a method is appropriate, but give it a descriptive name: calculateFoo() rather than getFoo(), for instance.

is the common practice in python to put __init__ in the beginning of class definition? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I wonder whether the recommended practice is to put init in the beginning of class definition or to put interface functions first:
class examp1:
def ifmethod1(self):
pass
def ifmethod2(self):
pass
def __init__(self):
pass
VS.
class examp1:
def __init__(self):
pass
def ifmethod1(self):
pass
def ifmethod2(self):
pass
A lot of people put it first, but every one can have their own opinion and you'll find arguments for both.
To me, being first is simply useful because it's helpful to know __init__ quickly.
In an effort to help coders of other languages grasp Python quickly, ibiblio.org says "__init__ is analogous to a constructor". In that spirit, it would often be put first — constructors in other languages are almost always listed first.
In one example, the Google style guide for Python, it is always listed first.

python shorten nested unmutable object variable names [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
This question is mostly just about curiosity.
As a java programmer myself, I often get frustrated by all the dots on nested variables. (There is kind of a de facto standard in java that any line with a variable with more than 2 dots needs to be re-written). Is there anyway to "rename" a nested variable such that I don't have to type all the dots everytime?
For example
class MyClass():
def my_func():
if not self.app.arguments.foo:
self.app.arguments.foo = 'bar'
print self.app.arguments.foo
Is there a way to write this such that I don't have to type the full name self.app.arguments.foo everytime?
Second, what is the standard 'pythonic' way of using variables as such. Would standard python nomenclature say just use all the dots all the time?
Extra notes
As I said, this is more about curiosity. So lets assume that I do not have access to module b. In other words I can not (more like don't want to) write getter and setter methods.
Also note that the self is important. I know I can do a rename with an import, but you cant import self.
You can use a temporary variable for all but the last item in a chain:
b = self.a.b
if b.c:
b.c = 'foo'
print b.c
If you weren't assigning, you could go all the way to c.
This is a little bit faster as well as being easier to write. However, it may or may not be easier to read.

Categories