« first day (2767 days earlier)      last day (2406 days later) » 

09:00
But iter([]) isn't even a generator. It doesn't have close, send, and throw.
@abarnert Any other tangible differences? inspect doesn't really count :p
Anyway, I think somewhere (maybe in the 2.2 "welcome to generators" doc that never got updated but is still linked somewhere from the current docs, like the 2.3 MRO docs) it says that while 0: yield is the officially recommended way to force a generator function. Using while False: instead of while 0: is an improvement, but yield None instead of yield, I have no idea why they did that.
0/
__iter__ doesn't have to return a generator, so I think return iter([]) is just fine and less hacky than if False: yield
If you return a generator (rather than a non-generator iterator), the only things I can think of off-hand are performance things (e.g., your frame isn't kept alive if you return a generator, unless the generator you return is a closure, but on the other hand there's no extra function-call cost).
09:07
Any good dupe targets for type hinting local vars for IDEs? Asking for stackoverflow.com/questions/50304479/…
insert valuable input here
I can't find it right now, but I remember reading that, while using isinstance per se is not bad, pythonic code avoids scenarios where it is necessary. Is that so?
True—in fact, usually __iter__ isn't a generator; usually if you don't have a custom iterator class, you just return a genexpr or an iter call.
@Arne in my experience, yes.
Yes, in Python, the general rule of thumb is that duck typing is best, inheritance relationships next best, explicit isinstance calls only when necessary, and type(x) is… checks only for really important micro-optimizations.
09:13
I have a lot of friends who complain about not seeing their types in python and not knowing what to do because of it but I don't really experience it much
other than the fact that official python documentation leaves much to be desired and really should list available methods etc more easily IMO
So I assume the difference is that my code style for python rarely needs to consider type directly
Anyway, gtg 0/
@IljaEverilä This question looks pretty good, though it's very old and most answers are outdated. There's a nice collection of ideas, and if you add an answer about type annotations on local variables, it'll be a good fit
(these friends are not experienced in python)
@Aran-Fey LGTM, will add the annotation stuff there.
I love flexible dynamic typing, like Python and Smalltalk. I also love powerful static typing, like Rust and Haskell. But most of the people I see complaining about dynamic typing in Python are people who use weakly-typed languages like C and Java, or dynamic-but-faking-it languages like Go, so I have no idea how to even make sense of their complaints.
I have a coworker who can go on for half an hour about how Go is great because "the compiler catches type errors" after we just spent 3 hours chasing down a null pointer exception that would have been impossible in a language with a real static type system or a 5-minute debug session in Python.
@IljaEverilä Looking at it again, it's asking specifically about function parameters, which is a bit suboptimal :/
09:21
Hmm that's unfortunately specific :\
That's one more question I can add to my list of questions that don't have proper duplicates... the list is growing long.
The only issue I have with Python's dynamic typing is that when reading unfamiliar Python code in a function, I'm not sure with what the class of an argument is.
anyone using mypy or similr here???
I have read about mypy and it seems promising. I might use it in the future.
There's also a static type checker that Facebook just releaseed.
I use it
09:24
It's called Pyre.
But only since a month or so. why?
@abarnert What does the code look like when you said, "inheritance relationships next best", in " duck typing is best, inheritance relationships next best"?
@Aran-Fey I'll try and find a better candidate. That question I linked is such a mess itself that it'd require quite a bit of editing, while trying not to change its spirit, and would be so much easier just to cv as dupe.
how to include class types in mypy ?
@Arne
What are "class types" and does "include" mean in this context?
09:28
d = None # type: MyClass works?
@SeanFrancisN.Ballais Usually, the class doesn't matter; it should be obvious what the object is supposed to do. For example, you often don't care whether you get a file, a list of strings, or an iterator that generates strings on the fly. Even if the guy who originally wrote the function was thinking of it as taking a file, I can still pass it a genexpr that transforms a list of strings.
@SeanFrancisN.Ballais But sometimes, you need a function that does different things with different types. Sometimes that inherently means type-switching—like a function that can take either a file-like iterable-of-strings, or a filename to open and read a file. But sometimes it's something you can express as with a base type and subclasses that do different things for the same methods.
Like asyncio(or Twisted) protocols: they have to have a suite of related methods that all work together in an appropriate way, so it makes sense to expect them to be subtypes of some Protocol type.
@IAmBatman Unless you need to work with older versions of Python, between to use d: MyClass = None.
old python2 code :/
Then yeah, comments. Although when I've used Mypy for porting a mess of 2.7 code to 3.x (really the only thing I've found it useful for…), I mostly only annotate parameters, not locals.
I use annotations quite a bit in 3.x code when they make something clearer or more readable, but, oddly, I rarely run Mypy on it.
Can someone explain to me why this question ranks getting down voted?
https://stackoverflow.com/questions/50326886/why-can-format-not-be-used-seperately-from-the-declaration

I realize the over-site was a simple, but it doesn't seem it warrants getting down voted and I'd like to avoid future "bad questions".
recbg
@Dave because you missed something, I'll leave a comment
well, it's what the accepted answer is...
09:41
Right, what I'm trying to understand is why that warrants the question itself being down voted based on posting criteria.
my point is that people probably thought that it reflects a fundamental misunderstanding with how python works, and for some people that's enough for a downvote. Or they thought that thoroughly reading a tutorial should cover this. I can only guess.
Alright... In other words it likely meets the posting criteria and just doesn't satisfy the human factor.. TY
Sometimes there are obvious reasons for downvotes, but other times Tim just lost his keys.
I think as far as objective points go your question is fine
Well, I just tried to install Pyre and it needs to update my OCaml and OPAM… and OPAM needs Python 2.7, and it will only use the Brew 2.7, not the Apple one. So, in order to install a tool for Python 3, I have to build Python 2.7.
It does make a change though from text.replace('a', 'e'); print(text) doesn't work...
09:43
I think that means it's time to go to sleep.
@AndrasDeak lol thanks. good read.
On a side note... there's mixed opinions about using the line continuation stuff... in your case it looks (imho) slightly nicer if you rely on adjacent strings being concatenated...eg:
f = (
    '{0} \n'
    '{1} \n'
    '{2}\n'
)
*string literals :)
yeah yeah... :)
absence of coffee... that's my excuse, okay? :p
sure thing ;)
you know where @idjaw lives; help yourself
09:48
Oops! I didn't notice that West, the [['a','b','c'],['d','e','f'],['g','h','i']] guy had commented on my answer. I'm a little concerned that he doesn't know how to convert a list of strings into a list of lists. If he doesn't know easy stuff like that, then those Algorithm X functions are going to be a total mystery.
@JonClements That was actually copy/paste code from a SO question relating to the topic prior to posting my question. I had been playing with this silly problem for almost an hour because it didn't make sense that it didn't work and I was missing the reassignment. The actual code wasn't used and looks nothing like that. The test code was much simpler. :)

Thanks though.
@Dave no worries... I upvoted anyway as I don't think it hurts to have a post thinking that .format works in place when it's tripped you up and str.replace has tripped plenty of others up for exactly the same reason as well...
I really wish we'd get more typing on returns on the python docs. I missed the last sentence even reading the official docs. Darn signature! :)
well strings are fundamentally immutable in python
it can't do anything other than return something new
Noted.
09:54
@AndrasDeak But I can do s += 'other' no...? :P
no you can't :P I forbid it
I decided to downvote stackoverflow.com/questions/43783857/… due to lack of research. The PhotoImage docs page has a big box down the bottom warning you of that "feature", and tells you the standard fix.
Okay? It's a year old :)
But the OP is still active
@PM2Ring "big box down the bottom" is topologically equivalent to a nonexistent box
09:59
@AndrasDeak wow... Meany Deak today aren't we? meep meep :p
late cbg
@JonClements wait until you see me tomorrow!
@abarnert I'm not comfortable saying that Python has dynamic typing. Sure, it looks like dynamic typing, if you think that Python names are variables. :) And although it's possible to change the type of a Python object apart from the ones that are defined in C), it's not exactly simple or obvious.
@AndrasDeak That line seems familiar. I remember a line like that somewhere in a book I read before titled, "Back to the User."
@AndrasDeak ahhh okies.... I'll skip tomorrow then and pop in Wednesday instead? :p
10:01
the jury is still out concerning Wednesday
Gtg for a bit. Got to study for an exam. WIeeee
@AndrasDeak So it is. :) It just got dupe-hammered, and I noticed the month, but not the year. Oh well...
I just wasn't sure what you wanted with the declaration. Public shaming or something else :D
From the question:
1
Q: How to to split() a string and pass it into an object's __init__() method?

Lauren MabeI am trying to create multiple instances of a Soda object using information from a file. The file is formatted like this Name,price,number Mtn. Dew,1.00,10 Coke,1.50,8 Sprite,2.00,3 My code I have is this (this is a function within main()): from Sodas import Soda def fillMachine(filename) ...

@AndrasDeak I felt a bit mean DVing since the question is clear and the OP has gone to the trouble of posting relevant code. And this "feature" is disconcerting when it catches you unawares, since code in a function behaves differently to identical code in the global context. But it's a fairly common question, and you'd think that people would check a one page doc before asking on SO.
10:13
it seems we need a canonical for classmethods/staticmethods factory methods like from_string. Something whose title is more amenable to being located than:
1186
Q: Meaning of @classmethod and @staticmethod for beginner?

user1632861Could someone explain to me the meaning of @classmethod and @staticmethod in python? I need to know the difference and the meaning. As far as I understand, @classmethod tells a class that it's a method which should be inherited into subclasses, or... something. However, what's the point of that...

perhaps something more actionable like
25
A: Multiple constructors: the Pythonic way?

9000You can't have multiple constructors, but you can have multiple aptly-named factory methods. class Document(object): def __init__(self, whatever args you need): """Do not invoke directly. Use from_NNN methods.""" # Implementation is likely a mix of A and B approaches. ...

which is a dupe of
242
Q: How to overload __init__ method based on argument type?

BaltimarkLet's say I have a class that has a member called data which is a list. I want to be able to initialize the class with, for example, a filename (which contains data to initialize the list) or with an actual list. What's your technique for doing this? Do you just check the type by looking at ...

Please don't one-box all those posts. Edit your messages and put at least a . next to the links
Although the first question above only wanted a single from_string function.
(@AndrasDeak: okay, will do in future, didn't know how to do that in one post)
I thought you could still edit at least some of those :/
is anyone here used to tensorflow?
(Will do next time). But anyone any comment? Seems like a gap in canonical
10:28
thanks
Serious question: Why do people always use filter(None, foo) instead of the more obvious filter(bool, foo)?
jpp
jpp
@Aran-Fey, Because the docs suggest None: "If function is None, the identity function is assumed, that is, all elements of iterable that are false are removed."
You use to be able to use None to map as well but that got removed...
@AndrasDeak first my snacks now this? My poor pantry
10:44
Gone already lol
@jpp So it's just a less readable version of filter(bool, foo), no?
jpp
jpp
@Aran-Fey, Hmm, well they seem equally readable, but that's subjective. I guess bool is more explicit.
I can't find any example where they aren't equivalent.
I guess using None is probably faster because it avoids some function calls, but that's micro-optimization
How long do I have to wait for a dupe hammerer to respond to my "that's not a suitable dupe" complaint before I'm legally allowed to reopen the question?
Is it bad style to overload __dict__? I have nested dataclasses, and want vars(a) to give {{val_1: 1}, {val_1: 2}} instead of {b(), b()}
jpp
jpp
@Aran-Fey Depends on how confident you are. If it's absolutely clear, then I'd comment + reopen. If there's some ambiguity comment + wait a few hours, I guess.
10:50
@Aran-Fey That's basically it.... it's the difference between if x and if bool(x)...
@jpp FWIW, the OP has "confirmed" that it doesn't solve their problem, but then again, how much do we trust an OP's judgment? :D
@Aran-Fey Is this a fresh hammering, or an old one?
It's 45 minutes young.
~40 minutes since my comment
I'd say give it an hour or so. But that's just my opinion, man. ;)
@jpp update your numpy :P
jpp
jpp
10:54
@AndrasDeak, Haha yeh I will at some point, just need the older version for now.
it won't make a difference but 1.11 << 1.14
there were even some nice recent goodies
jpp
jpp
Yup I know, lots more functionality has been added (especially some optional args). But shouldn't affect my specific question I hope.
probably not
11:05
@PM2Ring Alright, I'll try to be patient
*glances at clock every 10 seconds*
@Aran-Fey Do you intend to submit an answer?
No, it's already been answered. I just hate having a tab with that question open for an extended period of time; I want to get things resolved ASAP
It bothers me that defaultdict and OrderedDict have different capitalization
it bothers us all :(
isn't the handwaving excuse that one is in C, the other in python?
jpp
jpp
and Counter
It bothers me that only one is written in C
There should be one-- and preferably only one --obvious way to capitalize a name.
11:43
Sign my petition to make Python case-insensitive
NoOoOoOooo...
Sign my petition to make it so that @idjaw only stocks the best coffee and cookies...
it's only fitting for a puppy to have a _pet_​ition
Hey, Aran-Fey & I just hammered a question simultaneously. stackoverflow.com/questions/50329557/list-name-from-a-variable
only one person used a hammer....
11:56
nah, I voted unclear
makes sense, otherwise it'd be a bug
Ah, rightio.
you'd have two gold badges in the banner, if that were the case, I think
What's really quite interesting though... is when you have a mod-clash and it shows a post deleted by two diamonds
misleading message is misleading. "marked as duplicate by Aran-Fey, PM 2Ring" <- nope
11:57
yup, plenty of meta about that
@PM2Ring, actually when I got rid of the frame(ie, directly put the png in root), it worked fine. Guess they do support png now.
@AndrasDeak well no.... PM2 just wouldn't have had a close vote needed on the post...
yes, yes, but assuming there was a bug and a race condition :P
@Aran-Fey There are number of cases that lead to that. specially with equal voting (2-2-1, etc.)
just like your mod clash
jpp
jpp
11:58
I'm a bit concerned about the variable variables canonical (stackoverflow.com/questions/1373164/…). The answer is great, but there's not a great explanation why it's so bad. One answer does a decent job but it's half way down and talks more about coding effort.
@AndrasDeak could happen I guess (wouldn't be that surprised) but I've not seen it... seen deletions clash... never close votes
less of a race I guess
@AlphaRomeo Weird. I tested it (again) before I wrote that post. But I guess it's possible that Tkinter's been upgraded since I installed Python 3.6.0, which is the last time I updated Tkinter on my system.
@jpp set aside your concerns :P
95% of users will be fine if they follow the top few answers, and the skeptical 5% can be explicitly pointed to that one answer that explains why they shouldn't do that
jpp
jpp
@AndrasDeak, haha, here's me thinking a better answer might kill those dups.
12:01
one should always be hopeful :D
@jpp Honestly, I like the fact that you have to scroll to see the getattr and globals answers. If you tell a n00b "this is bad because X", they'll think "I don't give a flying yam about X". Just tell them "use a dict" and everyone's happy.
@jpp no, you'd just have yet another answer to that problem, a'la xkcd.com/927
jpp
jpp
@Aran-Fey FAir point, guess we cater for the masses.
@jpp It's not great, but it's better than it used to be. We deleted several of the worst answers. I'm not happy that Nadia's answer shows how to do it by man-handling globals(), but we can't really delete such a high-scoring answer.
jpp
jpp
@PM2Ring, What we can do is remove the support, e.g.. the non-needed comment "+1 answering the question as stated (even though it's horrible) "
I'm going to flag that is no longer required / chatty
it got 11 strange upvotes
12:04
not that strange
jpp
jpp
fine, I meant "upvotes I don't like" :p
I'm reading how to switch case and I did ok on 1 method, because I had to call 1 method but when I have to assignt it to a variable is that possible without putting it to method? I mean :
12:07
Are you using python?
I have this :
switcher = {
0: "zero",
1: "one",
2: "two",
}
return switcher[2] for instance ok?
then I have another code to clean up
that I have to do something like
switcher = {
0: new_value = "zero",
1: new_value = "one",
2: new_value="two",
}
return switcher[2]
nope, that won't work, that's not a legal dict
12:09
what you have there is not a "switch-case", because that doesn't exist in python
I know right
what do you want to accomplish?
new_value = switcher[2]?
So I keep the if-elif chain, right?
Why are you trying to set a variable that's never used? You return in the very next line.
I presume it's MCVEd away
12:11
new_value = list(value)[:]
if action == 1:
new_value = method()
elif action == 2:
new_value = method()
elif action == 3:
new_value = method()
elif action == 4:
new_value = method()
return tuple(new_state)
I have this
on a function
So, I'd like it to put in dic if it's more efficient
and faster
and if not, I'll keep this code out
Are those methods all different? You can put the methods in a dict, called a "dispatch dict"
More like method1(), method2(), method3(), I assume?
Yes yes
method1 method2
methods = {1: method1, 2: method2} etc (note the lack of parentheses: these are not function calls but the function objects themselves in the dict)
then new_value = methods[action]()
0/
12:12
o/
Let me test it Andras, hold on
@Skizo-ozᴉʞS what are you expecting new_value = list(value)[:] to do btw?
(I'm guessing it's not - create two shallow copies of an iterable and discard the first one after copying it)?
I don't want it to be a reference
I guess it does it :c
cabbage guys..
generate some junk so the garbage collector isn't bored ¯\_(ツ)_/¯
@Ski just the new_value = list(value) is enough :)
12:16
really stupid question here..
new_value = [*list(value)[:].copy()] * 1
I have a python files with some functions, I want to run them from another python file
@vaultah Needs more copy.copy and copy.deepcopy
@vaultah Laurel
I tried to import the mail file
I have a python files with some functions, I want to run them from another python file
12:17
But then I have to return tuple(new_value)
@Anarach Whats the code look like? Should just be "import file" then in the program file.func()
y'all can now step back a bit now, Skizo seems primarily an android/java dev :)
@ZackTarr Yeah I did that
@Skizo-ozᴉʞS that's independent of your original question
dispatch and the type of your returned value are separate things
Could you show us the code? Via like pastedb or something?
12:17
@Skizo-ozᴉʞS Yes, it's a little more efficient. But since your keys are small contiguous integers, why even bother with a dict? Just use a list of functions.
yeah , Just a moment , doing that
@Skizo yeah... where did new_state pop into that code anyway? :)
I have this
methods = {
1: foo(state),
2: foo(state),
3: foo(state),
4: foo(state),
}
new_state = list(state)[:]

return tuple(new_state = methods[action]())
@JonClements agreed. In the off chance I actually find some left for myself, I'd like the good stuff too
@Skizo-ozᴉʞS but why?
12:18
I need to returna tuple
Aima stuff ._.
But why are you calling the functions in the dict?
@Skizo-ozᴉʞS You can't put an assignment in a tuple call :)
what you have there is not what you think it is, it tries to pass a keyword argument called new_state
And your new_state seems to refer to multiple things... Could you look at it for a few minutes and create a clear MCVE of what you really mean/need?
I just don't understand what exactly you're trying to ask/do
10 mins ago, by Andras Deak
then new_value = methods[action]()
then return tuple(new_value) if that's what you meant
Are you learning python now or just trying to survive while you need to use it? :)
trying to survive while you need to use it :P
12:24
OK, then I won't suggest reading a nice tutorial :D
the first file has the functions which i want to call
I am importing it in second file
@AndrasDeak Why not? I can learn it for next time :)
but doesnt seem to trigger the 3 main functions.
@Skizo-ozᴉʞS oh, I just thought you didn't want to bother. By all means I suggest reading a nice tutorial :D
12:26
@Anarach It looks like all the functions are tabbed in to be seeded under the for loop. That might be the issue.
umm.. so I should envelop the for loop inside a function and call that one?
I would think so. Not sure when you would ever want to define a function inside of a loop like that.
also.. If its not much to ask ... Can someone give me some tips on the code which I have written.. I mean it works but i dont think its good way of writing code.
@AndrasDeak did you check it?
12:35
I'm waiting for you to do what I told you to do
that paste.ofcode link contains the same thing you asked earlier using different names. Substitute the names and it should work.
@Anarach Im confused on the need for it all to be inside a for loop. But Ill let one of the other guys in the room take a look as they will be able to give you better pointers to start with. Also looks like in my testing you can use functions from a file inside a for loop. So not sure why yours isnt working yet
Do you get any errors? Looks like you would run into some variables being undefined but Im not sure without trying to run your code.
@ZackTarr I mean , All the functions have to be executed for each excel file in the directory
so thats why I have put them in for loop
You have to put the function call in a loop, but not the function definition.
@Aran-Fey Wow
12:41
I would think, (keep in mind Im pretty green) that it would be better to call the function multiple times rather than define it. Not sure on the impact but I would assume there is.
I want to jumop from the window
please don't
joking
I know :P
I am sorry I took your time gentlemen
thank you for your help
12:42
No need to apologize. I hope you get it all working. Happy Coding!
Now .. where is that bottle of cynide.
Also.. THank you for the humble bundle thing
its really cool
@AndrasDeak I tried, but seems like it goes out of bound paste.ofcode.org/36qZQUhVHKadBR8ufUXkpzh
I was just looking through it
and its wonderful
@Skizo-ozᴉʞS that's not what I wrote
27 mins ago, by Aran-Fey
But why are you calling the functions in the dict?
12:46
I completely missed that ^ along with that message
@Aran-Fey to do new_state = state that is returned from method
@Skizo-ozᴉʞS please thoroughly read my original suggestion above
@Skizo-ozᴉʞS You just defined methods to be a dict. So why are you trying to use methods to index into a list?
the one where I told you it's called a dispatch dict
try to understand what's going on there and what the type of the relevant objects are
You won't be able to do it right unless you understand it.
no
I won't look
@AndrasDeak
I did not look that answer
12:48
look where? :D
you're free to do so, but I honestly think it'll benefit you in the long run if you understand what we're trying to show you
That's why I prefer it to not look that :P
yeah, good on you :)
It's easy if I copy paste and then I do that but in long time if I face with this again
I'll have nothing to do
12:50
yup, wish more people on SO realized that
Sometimes looking at working code is the easiest way to understand something :/
that might work if someone has a good understanding of the basics, but Skizo seems to be still in the phase of learning the basics
if you're confronted with an advanced pattern you might find it easier to give up because it looks daunting, even though it would be easy to comprehend in a few smaller steps and some guidance
I think it's hard to come up with that solution unless you already know the basics. It's a "huh, I didn't know you could do that" sort of thing, I think
It is. And we already told Skizo the solution :) They just need to understand the gist of it in order to adapt to their own problem
looking at the original and my suggestion in the surrounding 10 messages should help understanding dispatch, or let them ask specific questions if it doesn't
and then it's just a matter of replacing the names with what their real code contains
at least that's my take on the matter
either way if Skizo gives up we can still give them the answer ;)
But what @Aran-Fey said, why are you calling functions on the dic, I don't get this question
shouldn't be this?
option : what_to_do?
12:57
no, it's "option: what_function_should_be_called_later"
45 mins ago, by Andras Deak
methods = {1: method1, 2: method2} etc (note the lack of parentheses: these are not function calls but the function objects themselves in the dict)
the note ^ there says that it's not function calls but rather functions themselves in the dict
@AndrasDeak ...isn't that the same thing?
@Aran-Fey no, because "what_to_do" might imply "wash_the_dishes_as_in_now"
but if I have parameters on it I can do
this for instance : methods = {1: method1(value), 2: method2(value)}
@Skizo-ozᴉʞS if you have value by then you can, but the whole point is to reduce code repetition
if you only put functions into the dict you only need call them once
12:58
methods[key_specifying_method](value)
Oh
I guess I got it..
oh, and if your method calls have side-effects, you'd do those side-effects a lot of time
At least it runs
and if they don't have side-effects you're still doing unnecessary work

« first day (2767 days earlier)      last day (2406 days later) »