Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
view.py
variable = 'amount'
amount = 200
print(variable) #actual: 'amount' expected: 200
In such case, is it possible to use Variable Variable like PHP?
in php, I could do
$variable = 'amount';
$amount = 200;
echo $$variable;
I know it might be not a good practice, but sometimes it could provide very powerful features.
Thanks.
Neither Python nor Django's templates support variable variables... fortunately. It's a confusing language feature in PHP and we're better off without it - as you said, it's not even a good practice to use them.
Think of it as a bad habit in PHP that you'll be leaving behind now, even in PHP there are always better alternatives. Maybe someone thought it was a good idea at the time but modern best practices indicate otherwise.
If you feel the need for dynamic behaviour regarding variable names consider using a dictionary instead, see this post for details.
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 1 year ago.
Improve this question
I personally couldn't think of anything, but I'm wondering if there are some edge cases, where an older method might be somehow better.
It is often better to do logging calls via
log.info("some log {} data {} to be logged", arg1, arg2)
# will be `message.format(*args)` at the end of the day
vs.
log.info(f"some log {arg1} data {arg2} to be logged")
The reason is that if my logger is not configured to log INFO logs then the the second snippet does a potentially expensive string interpolation, converts the arguments to strings, etc. The first snippet does not do the interpolation and returns early without serializing the arguments.
Yes, when you need to reuse the same template string in multiple ways is better to use formatted strings. For example take a look at this question
f-strings aren't an option for anything you want to be localizable. An f-string is hard-coded directly into your program's source code, so you can't dynamically swap it out for a translated template string based on the user's language settings.
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 years ago.
Improve this question
Should i annotate types in my unit tests functions?
What also with overridden django methods like save and get or post functions in generic view?
This is arguably an opinion question but I think that there is a generally accepted answer, which is roughly "No".
One way of categorising programming languages is into statically typed and dynamically typed. Statically typed languages are generally more robust, especially for "programming in the large", and dynamically typed languages have advantages ito programming speed, and in modelling problems where it is beneficial to be able to accept data of various types. Type hints try to strike a balance. The rough rule being: if you are using the dynamic nature of the language to achieve something, don't worry about annotating it. If however, you are writing code that doesn't make specific use of the dynamic nature of the language, annotate
Perhaps to make the point clear, consider that if you DO annotate everything, well then why not just use Cython? Same effort but you actually get some speed up as well. The reason people use Python with annotations instead of Cython is that some problems are naturally better solved without specifying types.
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 7 years ago.
Improve this question
New programmer here!
I'm creating my first script on my own, and I have a particular function that is quite large, as in 50 lines.
I understand that theoretically a function can be as large as you need it to be, but etiquette-wise, where is a good place to stay under?
I'm using Python 2.something if that makes a difference.
A good rule of thumb (and it's more a guideline of thumb really) is that you should be able to view the entire function on one screen.
That makes it a lot easier to see the control flow without having to scroll all over the place in whatever editor you're using.
If you can't understand fully what a function does at first glance, it's probably a good idea to refactor chunks of code so that the more detailed steps are placed in their own, well-named, separate function and just called from this one.
However, it's not a hard-and-fast rule, you'll adapt your approach depending on your level of expertise and how complex the code actually is.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I want to create a serialized Python object from outside of Python (in this case, from Java) in such a way that Python can read it and treat it as if it were an object in Python. I'll start with simpler objects (int, float, String, and so on) but I'd love to know if this can be done with classes as well.
Functionality is first, but being able to do it quickly is a close second. The idea is that I have some data in Java land, but some business logic in Python land. I want to be able to stream data through the python logic as quickly as possible...right now, this data is being serialized as strings and I think this is fairly wasteful.
Thank you in advance
The best answer is to use a standardized format, such as JSON, and write up something to create the objects from that format in Python, and produce the data from Java. For simple things, this will be virtually no effort, but naturally, it'll scale up.
Trying to emulate pickle from within Java will be more effort than it's worth, but I guess you could look into Jython if you were really set on the idea.
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 want to have a flexible templating language that I can customize for a specific use case, and also make it simple enough for non-programmers to use. I looked at Cheetah; does anyone have any others and/or any thoughts on customization? Python would be great, but other language-implementations are also OK.
Jija2 is a templating library that I find easy to use
See: http://jinja.pocoo.org/docs/
See a previous SO question: What is the fastest template system for Python?
I like wheezy.template since it looks very similar to my python code. Its syntax is compact, expressive and clean. I was able to start right after a quick look at example. It amazing how intuitive it is. In addition it is fast.