I cannot initialize class in python - python

I have a class Flight, and I'm trying initialize it, but I have a syntax error in
print x=Flight(flightFromInput='nebrasca')
This is a content of my example file
class Flight:
flightFrom = None
flightTo = None
departureDate = None
arrivalDate=None
airline=None
serviceClass=None
departureAirport = None
arrivalAirport=None
#----------------------------------------------------------------------
def __init__(self,flightFromInput):
self.flightFrom = flightFromInput
print x=Flight(flightFromInput='nebrasca')
What is wrong with this code?

You should write
x = Flight(flightFromInput='nebrasca')
print x

In python an assignment statement doesn't return the assigned value. So you cannot use it within another statement. As the other answers suggested, you can work around this by printing x in a separate line.
Note, that there are exceptions though:
a = b = 0 # works
a = (b = 0) # does not work
The first case is a special case allowed for convenience when you want to assign the same value to multiple variables. In the second case you clearly tell the compiler that b=0 is a separate statement, but as it doesn't return a value the outer assignment to a leads to the resulting SyntaxError.
Hope this explains it a bit more clearly, why you should do print x after assigning it.

Contrary to C, in Python assignments are statements only and not expressions. Therefore they do not have their own value. Try this:
x = Flight(flightFromInput='nebrasca')
print x

Related

Python: using a function that returns two items in an If statement, without executing twice

I have a function I'm using to test in an if/then.
The issue is that I'm executing the function BOTH in the if conditional, and then again after the if statement because the function returns two items.
This just seems wasteful and I'm trying to think of ways to improve this. Here's a really basic version of what I'm trying to avoid: "True" is returned to allow the condition to pass, but then then "coolstuff()" is executed again to get more information from the function.
"coolstuff()" could possibly return false, so I can't use the returned string "stuff" as the test.
def coolstuff():
return True, "stuff"
if coolstuff()[0]:
coolthing = coolstuff()[1]
print coolthing
There's gotta be a better way to do this, no? My brain is melting a little as I try to hash it out.
I basically want to do something like this (invalid) syntax:
def coolstuff():
return True, "stuff"
if a, b == coolstuff() and a:
print b
Just collect both results into variables
a, b = fn()
if a:
# work with b
def coolstuff():
if valid:
return "stuff"
return None
data = coolstuff()
if data:
print(data)
Call the function and capture the entire returned value:
x = coolstuff()
Now you have access to both parts of the returned value, in x[0] and x[1].
Store it:
state, coolvar = coolstuff()
if state:
do_whatever(coolvar)
If in newer Python, you could use the dreaded walrus (but I prefer ti7's approach of just assigning in a separate line):
if (x := coolstuff())[0]:
print(x[1])

Python function, overwrite original variable

I have this function:
def icaocode(code):
c.execute("SELECT ICAO, LAT, LON FROM airports WHERE ICAO = ?", (code,))
result = c.fetchone()
if result is None:
print("No airport found with ICAO code", code)
sys.exit()
else:
print("Found", code)
[...]
Lets say I call this function with
icaocode(x)
How do I get the function to overwrite x with the results?
In function def:
def icaocode(code):
...
return code # new value
When calling:
x = icaocode(x)
Btw if the argument is mutable (like a list), you can overwrite it without returning the new value. If it's immutable (like a string, integer), you can't.
E.g.
def f(some_list):
some_list.append("something")
In this case
my_list = []
f(my_list)
my_list will be ["something"]
You can't overwrite the value of the parameter. That is, you can't change it to refer to another object. You can, however, change the object. There is an old thread on pass-by-value and pass-by-reference semantics in Python that you may find illuminating: https://stackoverflow.com/a/986145/399047
For example, you can append elements to a list that is passed in as a parameter. The following code:
def func(a_list):
a_list.append("some value")
l = [1,2,3]
print l
func(l)
print l
would give:
[1,2,3]
[1,2,3,"some value"]
In contrast, a string, cannot be modified. The following code:
def func2(a_str):
a_str += "suffix"
s = "test"
print s
func2(s)
print s
would give:
"test"
"test"
My recommendation, unless you have a good reason, is to avoid mutating your input parameters, and return the modified object instead. Side-effects can make for messy code.
If, at the end of all this you really want to modify your input parameter, one technique would be to wrap the code parameter inside another object. e.g.
def icaocode(code_list):
input_code = code_list[0]
[...]
# do something to input_code, assign result to
# output_code
[...]
code_list[0] = output_code
Then you would call with:
code_list = [code]
icaocode(code_list)
That said, this code is ugly to me, smells something awful, and I don't recommend it.
You can, but it is a horrible way to conduct business. Return the value instead, remember that you can return more than one value if you like. Here is however one way to return a value through a parameter. But don't use it.
>>> def a(b):
... b[0] = 'hi'
....
>>> c = ['nono']
>>> a(c)
>>> print(c)
['hi']

Python - summarize try-except statement

I want to summarize the following code. What it should do is check if the variable in the calculation is assigned. If not, then the result will be zero. Because I have hundreds of calculations like these I don't want to repeat the try-except for every calculation.
How could I do that?
a = 1
b = 2
d = 3
f = 2
try:
ab = a + b
except:
ab = 0
try:
ac = a - c
except:
ac = 0
try:
bg = b / g
except:
ac = 0
Write a function to do it, using a lambda (a one-line function) to defer the evaluation of the variables in case one of them doesn't exist:
def call_with_default(func, default):
try:
return func()
except NameError: # for names that don't exist
return default
ab = call_with_default(lambda: a+b, 0)
# etc.
You might benefit by using some sort of data structure (e.g. list or dictionary) to contain your values rather than storing them in individual variables; it's possible you could then use loops to do all these calculations instead of writing them all individually.
If you have a bunch of variables that might not even be defined, you probably don't really have a bunch of variables.
For example, if you're trying to build an interactive interpreter, where the user can create new variables, don't try to save each user variable as a global variable of the same name (if for no other reason than safety—what happens if the user tries to create a variable named main and erases your main function?). Store a dictionary of user variables.
Once you do that, the solutions suggested by Alexey and kindall will work:
def add_default(first, second, default):
try:
return variables[first] + variables[second]
except KeyError:
return default
variables['ab'] = add_default('a', 'b', 0)
If you really do need to mix in your code and user code at the same level, you can do it, by using globals() itself as your dictionary:
def add_default(first, second, default):
try:
return globals()[first] + globals()[second]
except KeyError:
return default
ab = add_default('a', 'b', 0)
However, using globals this way is almost always a sign that you've made a major design error earlier, and the right thing to do is back up until you find that error…
Meanwhile, from a comment:
I create a list of all my variables and loop through them if they have a value assigned or not. In case they have not I will set them to float('nan').
There's no way to create a list of variables (except, of course, by referencing them by name off globals()). You can create a list of values, but that won't do you any good, because there are no values for the undefined variables.
This is yet another sign that what you probably want here is not a bunch of separate variables, but a dictionary.
In particular, you probably want a defaultdict:
variables = collections.defaultdict(lambda: float('nan'))
For a more generic case you may use lambdas (though not too graceful solution):
def lambda_default(func, default, *args):
try:
return func(*args)
except:
return default
abc = lambda_default(lambda x, y: x + y * z, 0, a, b, c)
In case you have some commonly used functions, you may wrap them into one more def, of course:
def add_default(first, second, default):
return lambda_default(operator.add, 0, first, second)
ab = add_default(a, b, 0)

Python: as yet undefined variable called in function - but works?

I am still new to Python and have been reviewing the following code not written by me.
Could someone please explain how the first instance of the variable "clean" is able to be be called in the check_arguments function? It seems to me as though it is calling an as yet undefined variable. The code works but shouldn't that call to "clean" produce an error?
To be clear the bit I am referring to is this.
def check_arguments(ages):
clean, ages_list = parse_ages_argument(ages)
The full code is as follows...
def check_arguments(ages):
clean, ages_list = parse_ages_argument(ages)
if clean != True:
print('invalid ages: %s') % ages
return ages_list
def parse_ages_argument(ages):
clean = True
ages_list = []
ages_string_list = ages.split(',')
for age_string in ages_string_list:
if age_string.isdigit() != True:
clean = False
break
for age_string in ages_string_list:
try:
ages_list.append(int(age_string))
except ValueError:
clean = False
break
ages_list.sort(reverse=True)
return clean, ages_list
ages_list = check_arguments('1,2,3')
print(ages_list)
Python doesn't have a comma operator. What you are seeing is sequence unpacking.
>>> a, b = 1, 2
>>> print a, b
1 2
how the first instance of the variable "clean" is able to be be called in the check_arguments function?
This is a nonsensical thing to ask in the first place, since variables aren't called; functions are. Further, "instance" normally means "a value that is of some class type", not "occurrence of the thing in question in the code listing".
That said: the line of code in question does not use an undefined variable clean. It defines the variable clean (and ages_list at the same time). parse_ages_argument returns two values (as you can see by examining its return statement). The two returned values are assigned to the two variables, respectively.

Returning None or a tuple and unpacking

I am always annoyed by this fact:
$ cat foo.py
def foo(flag):
if flag:
return (1,2)
else:
return None
first, second = foo(True)
first, second = foo(False)
$ python foo.py
Traceback (most recent call last):
File "foo.py", line 8, in <module>
first, second = foo(False)
TypeError: 'NoneType' object is not iterable
The fact is that in order to correctly unpack without troubles I have either to catch the TypeError or to have something like
values = foo(False)
if values is not None:
first, second = values
Which is kind of annoying. Is there a trick to improve this situation (e.g. to so set both first and second to None without having foo returning (None, None)) or a suggestion about the best design strategy for cases like the one I present ? *variables maybe ?
Well, you could do...
first,second = foo(True) or (None,None)
first,second = foo(False) or (None,None)
but as far as I know there's no simpler way to expand None to fill in the entirety of a tuple.
I don't see what is wrong with returning (None,None). It is much cleaner than the solutions suggested here which involve far more changes in your code.
It also doesn't make sense that you want None to automagically be split into 2 variables.
I think there is a problem of abstraction.
A function should maintain some level of abstraction, that helps in reducing complexity of the code.
In this case, either the function is not maintaining the right abstraction, either the caller is not respecting it.
The function could have been something like get_point2d(); in this case, the level of the abstraction is on the tuple, and therefore returning None would be a good way to signal some particular case (e.g. non-existing entity). The error in this case would be to expect two items, while actually the only thing you know is that the function returns one object (with information related to a 2d point).
But it could also have been something like get_two_values_from_db(); in this case the abstraction would be broken by returning None, because the function (as the name suggest) should return two values and not one!
Either way, the main goal of using a function - reducing complexity - is, at least partially, lost.
Note that this issue would not appear clearly with the original name; that's also why it is always important to give good names to function and methods.
I don't think there's a trick. You can simplify your calling code to:
values = foo(False)
if values:
first, second = values
or even:
values = foo(False)
first, second = values or (first_default, second_default)
where first_default and second_default are values you'd give to first and second as defaults.
How about this:
$ cat foo.py
def foo(flag):
if flag:
return (1,2)
else:
return (None,)*2
first, second = foo(True)
first, second = foo(False)
Edit: Just to be clear, the only change is to replace return None with return (None,)*2. I am extremely surprised that no one else has thought of this. (Or if they have, I would like to know why they didn't use it.)
You should be careful with the x or y style of solution. They work, but they're a bit broader than your original specification. Essentially, what if foo(True) returns an empty tuple ()? As long as you know that it's OK to treat that as (None, None), you're good with the solutions provided.
If this were a common scenario, I'd probably write a utility function like:
# needs a better name! :)
def to_tup(t):
return t if t is not None else (None, None)
first, second = to_tup(foo(True))
first, second = to_tup(foo(False))
def foo(flag):
return ((1,2) if flag else (None, None))
OK, I would just return (None, None), but as long as we are in whacko-land (heh), here is a way using a subclass of tuple. In the else case, you don't return None, but instead return an empty container, which seems to be in the spirit of things. The container's "iterator" unpacks None values when empty. Demonstrates the iterator protocol anyway...
Tested using v2.5.2:
class Tuple(tuple):
def __iter__(self):
if self:
# If Tuple has contents, return normal tuple iterator...
return super(Tuple, self).__iter__()
else:
# Else return a bogus iterator that returns None twice...
class Nonerizer(object):
def __init__(self):
self.x=0
def __iter__(self):
return self
def next(self):
if self.x < 2:
self.x += 1
return None
else:
raise StopIteration
return Nonerizer()
def foo(flag):
if flag:
return Tuple((1,2))
else:
return Tuple() # It's not None, but it's an empty container.
first, second = foo(True)
print first, second
first, second = foo(False)
print first, second
Output is the desired:
1 2
None None
Over 10 years later, if you want to use default values I don't think there is a better way than the one already provided:
first, second = foo(False) or (first_default, second_default)
However, if you want to skip the case when None is returned, starting from Python 3.8 you can use the walrus operator (ie. assignment expressions) - also note the simplified foo:
def foo(flag):
return (1, 2) if flag else None
if values := Foo(False):
(first, second) = values
You could use an else branch to assign default values that's worse than the previous or option.
Sadly, the walrus operator does not support unparenthesized tuples so it is just a one line gain compared to:
values = foo(False)
if values:
first, second = values
One mechanism you can use to avoid the problem entirely when you have control of the method foo is to change the prototype to allow giving a default. This works if you are wrapping state but can't guarantee that a particular tuple value exists.
# self.r is for example, a redis cache
# This method is like foo -
# it has trouble when you unpack a json serialized tuple
def __getitem__(self, key):
val = self.r.get(key)
if val is None:
return None
return json.loads(val)
# But this method allows the caller to
# specify their own default value whether it be
# (None, None) or an entire object
def get(self, key, default):
val = self.r.get(key)
if val is None:
return default
return json.loads(val)
I found a solution for this problem:
Return None or return an object.
However you don't want to have to write a class just to return an object. For this, you can use a named tuple
Like this:
from collections import namedtuple
def foo(flag):
if flag:
return None
else:
MyResult = namedtuple('MyResult',['a','b','c']
return MyResult._make([1,2,3])
And then:
result = foo(True) # result = True
result = foo(False) # result = MyResult(a=1, b=2, c=3)
And you have access to the results like this:
print result.a # 1
print result.b # 2
print result.c # 3

Categories