Function that does nothing in Lua [duplicate] - python

This question already has an answer here:
is there a pass statement in Lua like in python
(1 answer)
Closed 8 years ago.
I've recently started learning Lua. The only other programming language I have some experience in is Python. In Python there is the "pass" function that does nothing. I was wondering what the equivalent (if any) of this would be in Lua.

In Python, pass is an important placeholder for incomplete code. It must exist because the syntax demands code to be present in some circumstances where you just want to declare a function or a if statement, a loop or whatever, but leave it open for future development while making the code compilable.
def myfunction(a, b, c):
pass # im doing this later
In Lua however this is not necessary. It is perfectly fine to end an if or a function without including any code. There is no need to exist a pass in Lua.
function myfunction(a, b, c)
-- im doing this later
end

Leave your conditional empty by doing this
if <condition> then end

Related

how to make new flow controls in python? [duplicate]

This question already has answers here:
Can you add new statements to Python's syntax?
(13 answers)
Closed 2 months ago.
Not sure how to explain, I mean statemtents like:
for i in l:
if a==b:
def x():
lambda x:
class spam:
while True:
basically those control statements that end with :
can I create novel ones? (like in snakemake that has a long list of new control statements)
I tried reading documentation, but could not find anything useful.
I just want to make some tools to help develop rules for snakemake.
I am currently using this:
class SM(object):
def __init__(self,**xargs):
self.items = xargs
def __getattribute__(self,attr):
return object.__getattribute__(self, "items")[attr]
input = SM(genome="Genome/genome.fa",
table="rmats/binding_strength.maxent.CLIP.csv")
table = pd.read_csv(input.table,index_col=0)
In that example I can use the class SM to emulate all the input, output, wildcard... then I can just move the code into its rule in the Snakefile without needing to manually edit all the inputs/wildcards/outputs...
However, I will still need to write the "input:".
Is there a way I could make:
input:
table="table.csv"
do
input=SM(table:"table.csv")
#or input=SM(**xargs)
Sorry, but no can do...You would have to modify the language implementation itself (the interpreter actually). When you are programming you are bound by the syntax of the language, you cannot modify the syntax "on the fly". It's not the same as e.g. defining functions, classes and whatnot.
Take a look at these:
Can you add new statements to Python's syntax?
How to make custom reserved keywords in python3
Here's the most comprehensive answer to this kind of questions imho:
https://stackoverflow.com/a/9108164/15923186

Checking argument values at runtime in python [duplicate]

This question already has answers here:
Obtaining closures at runtime [duplicate]
(1 answer)
How to open a closure in python?
(5 answers)
Closed 2 years ago.
I would like to know if there is any method to check whether two functions have the same arguments at runtime in python 3.
Basically, I have this function (func) that takes two arguments and perform some sort of computation. I want to check whether or not a and b have the same arguments' values at runtime
a = func(2, 3)
b = func(2, 3)
a.argsvalue == b.argsvalue
It is not feasible to run the code before and check the results because I am implementing a lazy framework. My main goal is to be able to understand what are the arguments of the function because there is one variable argument that I do not care but there is one static that is created before running the function.
##EDIT
I actually solved this problem using the inspect module (getclosure) for those who are interested. Thank you so much for the comments it helped me to familiarize myself with the terminology. I was actually looking for the closure, which I assigned dynamically.
when you do this - a.argsvalue == b.argsvalue you try to access a member of the value returned from the function.
so, if your "func" would return an object having the args you called it with (which sound like a weird thing to do) you would be able to access it.
anyway, if you need these values, just store them before sending them to the function, and then you can do whatever you want with them.

How can I forward-declare/prototype a function in Python? [duplicate]

This question already has answers here:
Does Python have class prototypes (or forward declarations)?
(6 answers)
Closed 8 years ago.
How do I prototype a method in a generic Python program similar to C++?
# Prototype
# Do Python prototyping
writeHello() # Gives an error as it was not defined yet
def writeHello():
print "Hello"
Python does not have prototyping because you do not need it.
Python looks up globals at runtime; this means that when you use writeHello the object is looked up there and then. The object does not need to exist at compile time, but does need to exist at runtime.
In C++ you need to prototype to allow two functions to depend on one another; the compiler then can work out that you are using the second, later-defined function. But because Python looks up the second function at runtime instead, no such forward definition is needed.
To illustrate with an example:
def foo(arg):
if not arg:
return bar()
def bar(arg=None):
if arg is not None:
return foo(arg)
Here, both foo and bar are looked up as globals when the functions are called, and you do not need a forward declaration of bar() for Python to compile foo() successfully.

How to avoid using 'self' so much [duplicate]

This question already has answers here:
How to avoid explicit 'self' in Python?
(11 answers)
python self-less
(3 answers)
Closed 9 years ago.
I am writing a program to simulate a small physical system and have become more and more annoyed as I write things like this:
K = 0.5 * self.m * self.v**2
In the case above, the equation is short and pretty understandable, but I have situations in which there is so much self that the whole thing ends up looking like a mess. I am aware that python always requires self to refer to class members, but is there a way to to make the code not look like a mosaic of self's?
EDIT: I usually do things such as:
var = self.var
and keep on using var instead of self.var. Later I do:
self.var = var
but this seems really stupid. What would be the pythonic way to solve this problem?
For messy parts I'd use Python modules and "module-level variables" instead of classes.
If all you want to do is save some keystrokes, you can always rename self to s:
class MyClass(object):
def kinetic_energy(s): # use s instead of self for brevity
return 0.5 * s.m * s.v**2
This saves you 3 characters per use of self. This goes against the standard convention, but nothing is stopping you from doing this. I would advice against doing this in general code, but it might be justified if it makes some very long formulas more readable. Do mention the unusual choice in a comment, in case anyone else has to read your code when you are long gone.
I guess it's possible to use some black magic in Python and come up with a context manager, which will take an object and put all its attribute in the context's locals(), and assign it back to object in the __exit__ function.
Found this https://code.google.com/p/ouspg/wiki/AnonymousBlocksInPython which may help.

Why did the Python developers make Python 3 have more parentheses than Python 2? [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 9 years ago.
Improve this question
For example in Python 3: print("Hello world")
And in Python 2: print "Hello world"
This makes me want to use Python 2 instead of Python 3, because it's simpler.
From http://www.python.org/dev/peps/pep-3105/
The following arguments for a print() function are distilled from a
python-3000 message by Guido himself:
print is the only application-level functionality that has a statement dedicated to it. Within Python's world, syntax is generally
used as a last resort, when something can't be done without help from
the compiler. Print doesn't qualify for such an exception.
At some point in application development one quite often feels the need to replace print output by something more sophisticated, like
logging calls or calls into some other I/O library. With a print()
function, this is a straightforward string replacement, today it is a
mess adding all those parentheses and possibly converting >>stream
style syntax.
Having special syntax for print puts up a much larger barrier for evolution, e.g. a hypothetical new printf() function is not too far
fetched when it will coexist with a print() function.
There's no easy way to convert print statements into another call if one needs a different separator, not spaces, or none at all. Also,
there's no easy way at all to conveniently print objects with some
other separator than a space.
If print() is a function, it would be much easier to replace it within one module (just def print(*args):...) or even throughout a
program (e.g. by putting a different function in builtin.print).
As it is, one can do this by writing a class with a write() method and
assigning that to sys.stdout -- that's not bad, but definitely a much
larger conceptual leap, and it works at a different level than print.
In Python2, print is a statement but in Python3, print is a function
Try this
print(print("Welcome"))
in python 2, it will produce SyntaxError: invalid syntax but in python3 it will produce
Welcome
None
It means that, first the inner print is invoked which prints Welcome and it returns None to the outer print which simply prints it.
Short simple answer: print was made a function in Python 3 as #kojiro suggested.

Categories