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 2 years ago.
Improve this question
This is not a problem I've encountered, but rather just a curiosity about best practices/style in python:
I see stuff like this quite often:
class A:
def __init__(self, qux):
"""...do whatever... maybe this is expensive - maybe it isn't - maybe I don't know if it's expensive"""
def get_baz(foo, bar=A(2)):
"""...do another thing..."""
return baz
I largely avoid function/method calls (like bar=A(2)) in function signatures, because this makes A.__init__ run on import. Do you think avoiding the running of code on import is best practice, or am I just OCD and it's totally fine to handle this on a case-by-case basis? I feel like there is an expectation (at least in the python community) that importing a package/module shouldn't take a significant amount of time/memory.
The common alternative to the above is slightly more verbose, but runs no extra code on import:
def get_baz(foo, bar=None):
if bar is None:
foo = A()
Which do you like more and why? Should I just not care?
(For context, I started thinking about this because importing an internal package at my company took 8 seconds, which was triggering.)
Related
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?
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).
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I was asked this question in a recent interview. And I know super is the way to get the super class, pass has no effect but to close a function or class declaration, yield is used in generator. But I have no idea how they are implemented in Python.
I searched and only get the answer for super: How is super() in Python 3 implemented?, anybody can answer for others?
super is not a keyword in python, it's a variable like int or range.
Keywords like pass, yield, def, if, ... are consumed by the parser to build an AST (Abstract-Syntax-Tree). You can play with the AST by yourself with the ast-module of python.
The AST is then turned in some lower level machine or virtual machine code by the code generator, ready for execution.
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.
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.