is it a good practice in python? function shortcuts? [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
is it a good practice that i usually (tend to) do in python?
making shortcut of functions for example:
p = print
and the i use:
p('hello world')
or
p(2**5)
does it affect on performance? What is your suggestion, is it OK?

A good practice in Python is: easy is better. You do not give much clarity in your code by doing that (and depending on the IDE-highlighting strategy, make it even worse for others). Furthermore, the choice of your shortcut, namely p, is really a bad idea. How about people writing code with prime numbers (and P is not a good idea too).
For performances, there is no gain I think. Since print is a built-in function, you actually add a variable inside globals() or locals(). In my opinion, this is 1) not a good practice to use shortcuts like that 2) not an improvement in performance.

I have no idea if it is a good practice in python, but in general I would say no.
It will take away readability for other readers of your code. If the codebase grows large and you replace a lot of functions with very short names you make it difficult to read in general.
Coming from many years of code maintenance if I was called out in the middle of the night to fix a code problem in production and found code like that I am pretty sure I would hunt you down in the morning and ask why you would ever do such a thing.
If you are lazy and doesn't want to write so much - get your self an editor that can complete code for you instead.

Related

Is it okay to use common abbreviations as variable names in Python? [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 6 years ago.
Improve this question
For example, would it be okay to use "pd" instead of "pandas" and "df" instead of "data_frame" in Python code?
On the one hand, using short variable names (less than 3 characters in length) is generally bad practice, stylistically, and from a maintainability perspective.
On the other hand, df and pd are rather ubiquitous for any Python programmer that uses the Pandas library.
What say you?
Stack Overflow really isn't the place for questions like this, may I suggest the Programmers Stack Exchange?
But since it is here, speaking as someone who has been forced to work with a massive C codebase with lots of 1-3 letter variable names and no documentation for the last few months, more verbose and descriptive variable names are probably better, especially if other people have to work with your code.
It would be better practice to use more letters, because if other people review your code, the variable pd would not make sense to them.
No, unless it's longer than 7 or 8 characters, as a rule of thumb. Get a proper IDE that can do some decent autocomplete, like PyCharm (autocomplete dialog automatically pops up and enter completes the variable).
df is too cryptic. Take a look at C++, with all its cryptic names itoa, itoa, stoi, etc.
PyCharm can do variable/class/function renaming automatically too.

Making a more pythonic wrapper around a non-pythonic 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 8 years ago.
Improve this question
I am going to use a library called socket.io-python-emitter (https://github.com/ziyasal/socket.io-python-emitter) in my project, however, the syntax it introduces really bothers me..
Example usage:
e = Emitter(...)
e.Emit('message')
e.In('group1').Emit('message')
e.In('group1').In('group2').Emit('message')
e.Of("/ns").In('group1').In('group2').Emit('message')
I guess the author have done it this way so the JavaScript/node.js people will feel more at home, but it it really makes it harder to code a dynamic emitter..
Ultimately, it should be (at least in my opinion):
e = Emitter(...)
e.emit('message')
e.emit('message', groups=['group1'])
e.emit('message', groups=['group1', 'group2'])
e.emit('message', groups=['group1', 'group2'], ns='/ns')
What is the best approach?
Create a wrapper around this lib? Seams like that would be ugly...
Fork the project and create a "pythonic" version? More ugly, and duplication.. Dont really want to do this.
Submit a patch to add a pythonic way to the upstream project?
Your third option seems (to me, of course) the good approach. I would first talk to the author(s)/maintainer(s) to see how they feel about the idea.
Since you create a new method (and use keywords arguments anyway), both syntaxes can coexist, so I think there is no need to fork the project or create a wrapper.

Do you have specific techniques for learning/memorizing python 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 8 years ago.
Improve this question
I am currently learning python and have been struggling learning new functions. There always seems to be a perfect match for functions I need and I never think of them until I see them utilized elsewhere. Is this normal? Does it happen to veteran programmers as well?
for example this snippet in learnpythonthehardway:
PHRASE_FIRST = False
if len(sys.argv) == 2 and sys.argv[1] == "english":
PHRASE_FIRST = True
I would have never thought of doing that without going in circles first.
Do you have specific mnemonic techniques for memorizing stuff? Can anyone share their insights?
Write code. Firstly, you will eventually memorise the common built-ins, like len, range, etc. There are probably a few that you will never remember at once, these are those you hardly ever use (eval, exec, compile, etc.) but that will depend on the kind of programs you are writing. That is normal when learning a programming language.
Importantly, learn how to find your way around the documentation. You might not memorise the exact argument-list for each function, but you should know the kind of features that are available, and how to look it up.
The standard library modules are another matter. You might only use 10% of them, although some people will use considerably more. Again, only learn what you need, maybe start at aspects of sys. Even so, there are obscure corners of sys that you might never need.
Don't try to remember everything, you don't need to.
Write code.
Just practice I guess.
Also, when doing something that seems "common", check the doc: https://docs.python.org/3/. Like if you want to split strings etc...
It's a pitty there is nothing like Hoogle (for Haskell) in Python (AFAIK)

Is python an ok language for a typing test sort of application as far as performance is concerned [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
Want to write a typing test application (GUI, windows). Is simple terms. app shows text, user types text, user gets typing speed result. Example of an online one
Is python an ok language to use with such a project? Note that some people can type upto 200 word/minute . thats a lot of keys per second. Can python keep up? I ask this coz I have heard that python being interpreted in somewhat slower than C++
and I have always wanted to learn (go beyond the getting started tutorial) python, so if performance isn't an issue I would go with python.
If python isn't suitable, kindly suggest some other language. I am kind of ok in php and know a bit of C#. C I am not good at.
Thanks
Allow me to state this rather into the blue: Python is well up to the task of handling 200 words / minute input. Plus it's a nice language for rapid prototyping.
In short, yes, python is an acceptable choice. The application you are creating does not appear to be doing anything incredibly intensive, and even if it was, that doesn't mean python would be a poor choice. It is used in tons of reliable apps with great performance.

What are features considerd as advanced python? [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 2 years ago.
Improve this question
I do basic python programming and now I want to get deep into language features. I have collected/considered the following to be advanced python capabilities and learning them now.
Decorator
Iterator
Generator
Meta Class
Anything else to be added/considered to the above list?
First, this thread should be community wiki.
Second, iterators and generators are pretty basic Python IMHO. I agree with you on decorators and metaclasses. But I'm not a very good programmer, so I probably find this more difficult to wrap my brain around than others.
Third, I would add threading/multiprocessing to the list. That's really tricky :)
There are some useful core concepts that can be added to your list, and that I would not necessarily teach in an introductory Python class (from the most common to the more specific):
the various protocols (sequence, iterator, context,…)
properties
packages
Some points related to important standard modules:
Making your classes compatible with the standard copy and pickle modules.
The first 3 are intermediate Python, not advanced. For advanced add the stuff in the Importing Modules and Python Language Services sections of the library reference.
I think you'll find that there isn't a good answer to your question. What's great about Python is that all of its features are fairly easy to understand. But there's enough stuff in the language and the library that you never get around to learning it all. So it really boils down to which you've had occasion to use, and which you've only heard about.
If you haven't used decorators or generators, they sound advanced. But once you actually have to use them in a real-world situation, you'll realize that they're really quite simple, and wonder how you managed to live without them before.

Categories

Resources