ChatGPT

Chat about non-baseball topics. No political discussions!
Post Reply
User avatar
ghostrunner
GOAT
Posts: 30752
Joined: April 18 06, 9:40 pm

Re: ChatGPT

Post by ghostrunner »

Arthur Dent wrote:
March 20 24, 3:31 pm
That code seems fine. What am I missing?
My bad. I pasted the last question I asked instead of the one I meant.

Here's what I was going off of. Same dictionary, so:

Code: Select all

pet_dictionary = {"dogs": ["Yorkie", "Collie", "Bulldog"], "cats": ["Persian", "Scottish Fold", "Siberian"], "rabbits": ["Angora", "Holland Lop", "Harlequin"]}  

print(pet_dictionary.get("dogs", 0))

#that returns

['Yorkie', 'Collie', 'Bulldog']
So I tried to retrieve more than by adding "cats", which doesn't work, so I asked:
can the get method only retrieve one key?
Coach:
No, the get method can retrieve multiple keys.

The syntax for the get method is:

get(key, default=None)
The key parameter is the key that you want to retrieve. The default parameter is the value that will be returned if the key does not exist.

For example, the following code retrieves the value for the key "name" from the dictionary my_dictionary:

value = my_dictionary.get("name")
If the key "name" does not exist in the dictionary, the value None will be returned.

You can also use the get method to retrieve multiple keys at once. For example, the following code retrieves the values for the keys "name", "age", and "city" from the dictionary my_dictionary:

values = my_dictionary.get("name", "age", "city")
There are multiple other ways of doing this but the last part isn't true/doesn't work.

Probably could have been a better worded question on my part, because really I'm looking to retrieve multiple values using the keys. But it's still coming back with seemingly made up information.

User avatar
Joe Shlabotnik
Hall Of Famer
Posts: 24223
Joined: October 12 06, 2:21 pm
Location: Baseball Ref Bullpen
Contact:

Re: ChatGPT

Post by Joe Shlabotnik »

ghostrunner wrote:
March 20 24, 6:37 pm

So I tried to retrieve more than by adding "cats", which doesn't work, so I asked:
can the get method only retrieve one key?
Coach:
No, the get method can retrieve multiple keys.

The syntax for the get method is:

get(key, default=None)
The key parameter is the key that you want to retrieve. The default parameter is the value that will be returned if the key does not exist.

For example, the following code retrieves the value for the key "name" from the dictionary my_dictionary:

value = my_dictionary.get("name")
If the key "name" does not exist in the dictionary, the value None will be returned.

You can also use the get method to retrieve multiple keys at once. For example, the following code retrieves the values for the keys "name", "age", and "city" from the dictionary my_dictionary:

values = my_dictionary.get("name", "age", "city")
There are multiple other ways of doing this but the last part isn't true/doesn't work.

Probably could have been a better worded question on my part, because really I'm looking to retrieve multiple values using the keys. But it's still coming back with seemingly made up information.
God, I love Python.

list comprehensions for the win (if you want a one liner):

pet_list = [my_dictionary.get(pets, None) for pets in ["dogs", "cats"]]

Arthur Dent
Hall Of Famer
Posts: 12536
Joined: April 25 06, 6:43 pm
Location: Austin

Re: ChatGPT

Post by Arthur Dent »

That makes more sense, and yes you definitely can't trust that AI outputs are accurate.

Was not familiar with Python's dict.get() method. I feel like in most cases you'd be better off using the standard mydict[key] over mydict.get(key). If you were trying to find something in the dictionary that isn't there, it's likely because you made a mistake and an immediate exception is less confusing than a silent None that travels on to screw something else up later.

If I did want this, I'd probably just write it explicitly:
mydict[key] if key in mydict.keys() else None

User avatar
Joe Shlabotnik
Hall Of Famer
Posts: 24223
Joined: October 12 06, 2:21 pm
Location: Baseball Ref Bullpen
Contact:

Re: ChatGPT

Post by Joe Shlabotnik »

OR you rely on the NONE possibility to take care of those problems without excepting. Using get method is cleaner and more "pythonic". Another reason to use get and handle the None case yourself is to implement your own Exception classes which can give your users application specific error handling.

Arthur Dent
Hall Of Famer
Posts: 12536
Joined: April 25 06, 6:43 pm
Location: Austin

Re: ChatGPT

Post by Arthur Dent »

Maybe everyone else is familiar with this get() method, in which case great. To me, if I saw this, it just adds a minute of unnecessary head scratching since get is such an empty method name. I would see it and think this must be a variable of some other type since you get things out of dictionaries with the index operator.

I agree that if you have a case where you might be intentionally looking for things in the dictionary that aren't there, returning None instead of catching a KeyError is good. My feeling is just that it in my personal experience, if I'm doing doing this, it's probably because I made a logic error, typo, or whatever, and Python throwing the exception at me is a good thing. Using get() seems like the special case and a potential bad habit. In my mind, writing the special case explicitly, if it's needed, is more clear.

But maybe everyone but me sees the need for this routinely and uses get() all the time, in which case it's whatever.

User avatar
Joe Shlabotnik
Hall Of Famer
Posts: 24223
Joined: October 12 06, 2:21 pm
Location: Baseball Ref Bullpen
Contact:

Re: ChatGPT

Post by Joe Shlabotnik »

Arthur Dent wrote:
March 21 24, 11:07 am
Maybe everyone else is familiar with this get() method, in which case great. To me, if I saw this, it just adds a minute of unnecessary head scratching since get is such an empty method name. I would see it and think this must be a variable of some other type since you get things out of dictionaries with the index operator.

I agree that if you have a case where you might be intentionally looking for things in the dictionary that aren't there, returning None instead of catching a KeyError is good. My feeling is just that it in my personal experience, if I'm doing doing this, it's probably because I made a logic error, typo, or whatever, and Python throwing the exception at me is a good thing. Using get() seems like the special case and a potential bad habit. In my mind, writing the special case explicitly, if it's needed, is more clear.

But maybe everyone but me sees the need for this routinely and uses get() all the time, in which case it's whatever.
Understood. I write commercial python applications and utilities used by internal corporate customers so customized exceptions are a thing for us.

Arthur Dent
Hall Of Famer
Posts: 12536
Joined: April 25 06, 6:43 pm
Location: Austin

Re: ChatGPT

Post by Arthur Dent »

Sure. The code I write typically has a small group audience, which is a different game. When I screw up, the program crashes but gives me a good error message with which to find the problem is a fine approach.

That said, I still define my own exceptions, but only for cases where I explicitly anticipate some issues and want to pre-define the reaction. Like I'll create an exception for the case when the user input is in the wrong format, or the item wasn't in the database, or whatever. When it's just a programming error, I don't try and catch the exception or try to prevent it from being raised.

I'm sort of curious how practice differs for more commercial grade code.

User avatar
Joe Shlabotnik
Hall Of Famer
Posts: 24223
Joined: October 12 06, 2:21 pm
Location: Baseball Ref Bullpen
Contact:

Re: ChatGPT

Post by Joe Shlabotnik »

Arthur Dent wrote:
March 21 24, 12:24 pm
Sure. The code I write typically has a small group audience, which is a different game. When I screw up, the program crashes but gives me a good error message with which to find the problem is a fine approach.

That said, I still define my own exceptions, but only for cases where I explicitly anticipate some issues and want to pre-define the reaction. Like I'll create an exception for the case when the user input is in the wrong format, or the item wasn't in the database, or whatever. When it's just a programming error, I don't try and catch the exception or try to prevent it from being raised.

I'm sort of curious how practice differs for more commercial grade code.
I don't think we are too far off philosophically here. I agree totally with your second paragraph. The reason I like using get now instead of a bald dictionary reference that will exception out if it doesn't exist is really two-fold:

1. It makes me think about handling the error. Which, in our applications, we definitely must do to protect users from themselves (your paragraph 2)
2. That second parameter to the get function can be anything including a function. So, for me, its cleaner to have a get call that wrap up both success and fail paths on one line. Completely personal preference.

User avatar
ghostrunner
GOAT
Posts: 30752
Joined: April 18 06, 9:40 pm

Re: ChatGPT

Post by ghostrunner »


User avatar
AdmiralKird
MBA, CPA, CFA, CFP, JD, PE, MD
Posts: 10169
Joined: March 7 07, 4:50 pm
Location: Tycho Crater, Luna

Re: ChatGPT

Post by AdmiralKird »

I don't see how that lawsuit is a slamdunk. You wouldn't successfully sue someone for subscribing and reading every article in the New York Times and then writing an article that sounds like it might appear in a fictional NYT. I just don't understand the lawsuits based on suing for learning, so long as the developers paid to access the original content.

I'm also not sure how an AI model that is not hooked up to the internet for real-time data or live content is able to damage timely investigative reporting?

Post Reply