-
Notifications
You must be signed in to change notification settings - Fork 41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
init = 0 if select_ == 1 else 0 is always == 0 #3
Conversation
Shouldn't it be: init = 1 if select_ == 1 else 0
Or rather ? init = 1 if len(select_) == 1 else 0 init = 1 if len(select_) > 0 else 0 Because select_ == 1 (a list) in a console test seems == 1 either if it's empty [] or if it has elements. |
Thanks Todor, so you are saying list != len(list)? I was told otherwise. |
Yes, at least I think so and this is what my tiny code tests show (run it if you wish): I haven't seen your syntax, so I wasn't sure if it were a rarely used feature - e.g. type inferring as lenght when there were a comparison with scalar. To me it's not reasonable, though. Even if it's "None" it seems to return "1". There's similar syntax in C/C++, for a dynamic variable (pointer) which has not allocated memory yet and is set initially to 0 (NULL). Then Var==0 would return TRUE (which is 1 in C) and after allocating memory, the expression would return FALSE, because the pointer would hold a valid address. |
Thanks Todor, I corrected it. Also note that I sometimes have operations that directly change values in tuples, which is not legal. I guess I have to either unpack and repack these tuples, or convert them into lists. |
Right, this is a test with sample reassignment: https://github.com/Twenkid/CogAlg/blob/master/test.py It's the expressions: dP[7]=, vP[7]=, alt_P[7] ... Also if alt: semantics was suspicious**, similarly to if selection_, I assume you meant "any of the components is different than 0". Tests confirmed, it's always True even if it's 0,0,0, see also the cases in test.py. Another solution for such cases when you watch for any change is to have a separate flag "changed". |
Thanks, I changed: |
if alt[0]: Yes. (I didn't know yet for which exact element you expected a non-zero value.) Tuples: alt = 1,0,0 There are examples for more complex cases, tuple of tuples. If there is a complex structure with mixed tuples, lists, tuples of tuples etc., however I don't know could it be so straight or an explicit type-reflection would be needed (slow), that may require more checking. (But repetitive conversion may hit performance.) |
Also, besides numpy, if it can't be installed for some reason in a Pypy environment for example, there are also simple Python "array" module, but it holds same-type elements, like for input images: https://docs.python.org/3/library/array.html |
Thanks. I guess I could just initialize all tuples as lists (=[]), and they will stay lists after assignment? |
Would lists stay lists after a tuple is assigned to the variable a = []? AFAIK - no. This code snippet turns a list into a tuple: b = []
(Except if the type hints, TypeAlias, NewType don't work for it with some special constraining usage - see below). However assignment of the components from lists seems the same: def f4(): [1, 45, 24] The structure - well, I think the closest is Class, maybe not exactly as you asked, as of being sure that all instances are same-size, maybe they would, but I don't know would be of much advantage, because they would be likely to be complex objects with dynamic variables with dynamic size and their attributes may be not locally allocated in contiguous RAM-block, I don't know. Actually lists and even int are also instances of classes. Type hints, type aliases and NewType can be used, though, but I don't have experience with them, maybe it may serve as building a more C-like/C++ structures/classes: However it's not yet clear does strict type-checking is applied or forced type-conversion when a not appropriate type is assigned. One simple tests didn't do:
... Also, I think performance-wise and precision-wise care should be taken about int/float mixing. I noticed that in some places you use integer division //.
(There would be type conversions + the calculations and the other overhead). In current implementation, the image input is int, but once division kicks in: comp_P, fork_eval, the variables become float unless they were explicitly converted to int with int(...) c = int(a/b). Such type conversion may apply also if the variables are class-attributes, also potentially change the type/recreate the variable (which itself is a class instance). One way to partially limit this is to filter all accesses to class' attributes through methods (functions), however Python class elements by default are "public", there is a convention for "private" by adding underscores: classname, but it's for the developers, technically they are also accessible. See 9.6. Private Variables. |
I missed to mention the "struct" (import struct) - because it requires pack/unpack to binary representation of C-types and it seems cumbersome. http://www.learntosolveit.com/python/design_struct_example.html Also, do you know about the named tuples? |
I don't get it. A list can take anything, why would assignment change it to tuple? Do you get a type error if you try append, +=, list[n] = x, etc., after assigning list = a,b,c? |
list+= doesn't produce a type error, but the tuple is flatten and the items in the tuple are added as individual ones. I assume that the k[]; k = (1,2,3) type-change is due to the assignment operator - perhaps the type on the right hand has a higher priority.
Append and addressing an index work as expected intuitively:
Error if out of range: l[10] = alt |
So, it seems that there is no type change, it's mutable, so it is still a
list. Anyway, I will just convert it to list if it gives me an error.
…On Fri, Feb 2, 2018 at 4:51 PM, Todor Arnaudov ***@***.***> wrote:
*list+*= doesn't produce a type error, but the tuple is flatten and the
items in the tuple are added as individual ones. I assume that the k[]; k =
(1,2,3) type-change is due to the assignment operator - perhaps the type on
the right hand has a higher priority.
l+=(5,6,4)
l
[1, 0, 0, 5, 6, 4]
*Append and addressing an index* work as expected intuitively:
alt
(1, 0, 0)
l.append(alt)
l
[1, 0, 0, 5, 6, 4, (1, 0, 0)]
l[0] = alt
l
[(1, 0, 0), 0, 0, 5, 6, 4, (1, 0, 0)]
l[5]=alt
l
[(1, 0, 0), 0, 0, 5, 6, (1, 0, 0), (1, 0, 0)]
*Error if out of range:*
l[10] = alt
Traceback (most recent call last):
File "", line 1, in
IndexError: list assignment index out of range
...
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#3 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AUAXGVmvyadWmowUTyMiz8y_VbFn5hCeks5tQ4NXgaJpZM4RxwEc>
.
|
No, I think there is a type change when there's a direct assignment of a tuple to a var declared as list.
It turns into a tuple. Yes, if you append to a list, or insert into it, it's still a list. |
Ok, it's append or insert then.
…On Fri, Feb 2, 2018 at 5:22 PM, Todor Arnaudov ***@***.***> wrote:
No, I think there is a type change when there's a direct assignment of a
tuple to a var declared as list.
At least that's the behaviour I see:
a = [1,2]
alt = 5,6,7
a = alt
a
(5, 6, 7)
a+=5
Traceback (most recent call last):
File "", line 1, in
TypeError: can only concatenate tuple (not "int") to tuple
a
(5, 6, 7)
a+=(10,9,8)
a
(5, 6, 7, 10, 9, 8)
It turns into a tuple.
Yes, if you append to a list, or insert into it, it's still a list.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#3 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AUAXGWOtzXoP0F69FxKUwVGboOcDClRNks5tQ4qIgaJpZM4RxwEc>
.
|
Shouldn't it be: init = 1 if select_ == 1 else 0