Re: Configuring an object via a dictionary

2024-03-20 Thread Dan Sommers via Python-list
On 2024-03-20 at 09:49:54 +0100, Roel Schroeven via Python-list wrote: > You haven't only checked for None! You have rejected *every* falsish value, > even though they may very well be acceptable values. OTOH, only you can answer these questions about your situations. Every application, every i

Re: Configuring an object via a dictionary

2024-03-20 Thread Roel Schroeven via Python-list
Op 19/03/2024 om 0:44 schreef Gilmeh Serda via Python-list: On Mon, 18 Mar 2024 10:09:27 +1300, dn wrote: > YMMV! > NB your corporate Style Guide may prefer 'the happy path'... If you only want to check for None, this works too: >>> name = None >>> dafault_value = "default" >>> name or default

Re: Configuring an object via a dictionary

2024-03-19 Thread Pokemon Chw via Python-list
It's too complicated, there's no need for this def __init__(self, config): self.__dict__ = config self.connection = None """ other code . """ Note that you need to keep the fields in the config dict named the same as the fields you want to be assigne

Re: Configuring an object via a dictionary

2024-03-18 Thread Loris Bennett via Python-list
Tobiah writes: > I should mention that I wanted to answer your question, > but I wouldn't actually do this. I'd rather opt for > your self.config = config solution. The config options > should have their own namespace. > > I don't mind at all referencing foo.config['option'], > or you could mak

Re: Configuring an object via a dictionary

2024-03-18 Thread Tobiah via Python-list
I should mention that I wanted to answer your question, but I wouldn't actually do this. I'd rather opt for your self.config = config solution. The config options should have their own namespace. I don't mind at all referencing foo.config['option'], or you could make foo.config an object by its

Re: Configuring an object via a dictionary

2024-03-18 Thread Tobiah via Python-list
ith a view to asking forgiveness rather than permission, is there some simple way just to assign the dictionary elements which do in fact exist to self-variables? class Foo(): def __init__(self, config): for key, val in config.iteritems(): setattr(self

Re: Configuring an object via a dictionary

2024-03-18 Thread Anders Munch via Python-list
dn wrote: >Loris Bennett wrote: >> However, with a view to asking forgiveness rather than >> permission, is there some simple way just to assign the dictionary >> elements which do in fact exist to self-variables? > >Assuming config is a dict: > > self.

RE: Configuring an object via a dictionary

2024-03-17 Thread AVI GROSS via Python-list
If we are bringing up other languages, let's return to what was part of the original question. How van a dictionary be used in python if your goal is to sort of use it to instantiate it into a set of variables and values inside the local or global or other namespaces? Can we learn any

Re: Configuring an object via a dictionary

2024-03-17 Thread dn via Python-list
On 18/03/24 04:11, Peter J. Holzer via Python-list wrote: On 2024-03-17 17:15:32 +1300, dn via Python-list wrote: On 17/03/24 12:06, Peter J. Holzer via Python-list wrote: On 2024-03-16 08:15:19 +, Barry via Python-list wrote: On 15 Mar 2024, at 19:51, Thomas Passin via Python-list wrote

Re: Configuring an object via a dictionary

2024-03-17 Thread Peter J. Holzer via Python-list
On 2024-03-17 17:15:32 +1300, dn via Python-list wrote: > On 17/03/24 12:06, Peter J. Holzer via Python-list wrote: > > On 2024-03-16 08:15:19 +, Barry via Python-list wrote: > > > > On 15 Mar 2024, at 19:51, Thomas Passin via Python-list > > > > wrote: > > > > I've always like writing using

Re: Configuring an object via a dictionary

2024-03-16 Thread dn via Python-list
On 17/03/24 12:06, Peter J. Holzer via Python-list wrote: On 2024-03-16 08:15:19 +, Barry via Python-list wrote: On 15 Mar 2024, at 19:51, Thomas Passin via Python-list wrote: I've always like writing using the "or" form and have never gotten bit I, on the other hand, had to fix a prod

Re: Configuring an object via a dictionary

2024-03-16 Thread Peter J. Holzer via Python-list
On 2024-03-16 08:15:19 +, Barry via Python-list wrote: > > On 15 Mar 2024, at 19:51, Thomas Passin via Python-list > > wrote: > > I've always like writing using the "or" form and have never gotten bit > > I, on the other hand, had to fix a production problem that using “or” > introducted. >

Re: Configuring an object via a dictionary

2024-03-16 Thread dn via Python-list
On 16/03/24 21:15, Barry via Python-list wrote: On 15 Mar 2024, at 19:51, Thomas Passin via Python-list wrote: I've always like writing using the "or" form and have never gotten bit I, on the other hand, had to fix a production problem that using “or” introducted. I avoid this idiom beca

Re: Configuring an object via a dictionary

2024-03-16 Thread Thomas Passin via Python-list
On 3/16/2024 8:12 AM, Roel Schroeven via Python-list wrote: Barry via Python-list schreef op 16/03/2024 om 9:15: > On 15 Mar 2024, at 19:51, Thomas Passin via Python-list   wrote: > > I've always like writing using the "or" form and have never gotten bit I, on the other hand, had to fix a p

Re: Configuring an object via a dictionary

2024-03-16 Thread Roel Schroeven via Python-list
Barry via Python-list schreef op 16/03/2024 om 9:15: > On 15 Mar 2024, at 19:51, Thomas Passin via Python-list wrote: > > I've always like writing using the "or" form and have never gotten bit I, on the other hand, had to fix a production problem that using “or” introducted. I avoid this

Re: Configuring an object via a dictionary

2024-03-16 Thread Barry via Python-list
> On 15 Mar 2024, at 19:51, Thomas Passin via Python-list > wrote: > > I've always like writing using the "or" form and have never gotten bit I, on the other hand, had to fix a production problem that using “or” introducted. I avoid this idiom because it fails on falsy values. Barry -- htt

RE: Configuring an object via a dictionary

2024-03-15 Thread AVI GROSS via Python-list
of control. -Original Message- From: Python-list On Behalf Of Dan Sommers via Python-list Sent: Friday, March 15, 2024 5:33 PM To: python-list@python.org Subject: Re: Configuring an object via a dictionary On 2024-03-15 at 15:48:17 -0400, Thomas Passin via Python-list wrote: > [...] And I sup

Re: Configuring an object via a dictionary

2024-03-15 Thread Thomas Passin via Python-list
On 3/15/2024 5:33 PM, Dan Sommers via Python-list wrote: On 2024-03-15 at 15:48:17 -0400, Thomas Passin via Python-list wrote: [...] And I suppose there is always the possibility that sometime in the future an "or" clause like that will be changed to return a Boolean, which one would expect an

Re: Configuring an object via a dictionary

2024-03-15 Thread dn via Python-list
question, the __init__(), are at least equally-important considerations (see below)! I could of course simply test each element of the dictionary before trying to use. I could also just write self.config = config but then addressing the elements will add more clutter to the code. By

Re: Configuring an object via a dictionary

2024-03-15 Thread Dan Sommers via Python-list
On 2024-03-15 at 15:48:17 -0400, Thomas Passin via Python-list wrote: > [...] And I suppose there is always the possibility that sometime in > the future an "or" clause like that will be changed to return a > Boolean, which one would expect anyway. Not only is the current value is way more usefu

Re: Configuring an object via a dictionary

2024-03-15 Thread Thomas Passin via Python-list
entifier'] self.group_base = config['group_base'] self.group_identifier = config['group_identifier'] self.owner_base = config['owner_base'] However, some entries in the configuration might be missing. What is the best way of dealing with t

Re: Configuring an object via a dictionary

2024-03-15 Thread Grant Edwards via Python-list
x27;] >> self.user_identifier = config['user_identifier'] >> self.group_base = config['group_base'] >> self.group_identifier = config['group_identifier'] >> self.owner_base = config['owner_base'] >> >> However,

Re: Configuring an object via a dictionary

2024-03-15 Thread Thomas Passin via Python-list
;group_identifier'] self.owner_base = config['owner_base'] However, some entries in the configuration might be missing. What is the best way of dealing with this? I could of course simply test each element of the dictionary before trying to use. I could also just write sel

Re: Configuring an object via a dictionary

2024-03-15 Thread Mats Wichmann via Python-list
On 3/15/24 03:30, Loris Bennett via Python-list wrote: Hi, I am initialising an object via the following: self.source_name = config['source_name'] config.get('source_name', default_if_not_defined) is a common technique... -- https://mail.python.org/mailman/listinfo/python-list

Configuring an object via a dictionary

2024-03-15 Thread Loris Bennett via Python-list
x27;] However, some entries in the configuration might be missing. What is the best way of dealing with this? I could of course simply test each element of the dictionary before trying to use. I could also just write self.config = config but then addressing the elements will add m

Re: If a dictionary key has a Python list as its value!

2024-03-07 Thread Mats Wichmann via Python-list
On 3/7/24 07:11, Varuna Seneviratna via Python-list wrote: If a dictionary key has a Python list as its value, you can read the values one by one in the list using a for-loop like in the following. d = {k: [1,2,3]} for v in d[k]: print(v) No tutorial describes this, why? What is the

Re: If a dictionary key has a Python list as its value!

2024-03-07 Thread MRAB via Python-list
On 2024-03-07 14:11, Varuna Seneviratna via Python-list wrote: If a dictionary key has a Python list as its value, you can read the values one by one in the list using a for-loop like in the following. d = {k: [1,2,3]} for v in d[k]: print(v) No tutorial describes this, why? What is

If a dictionary key has a Python list as its value!

2024-03-07 Thread Varuna Seneviratna via Python-list
If a dictionary key has a Python list as its value, you can read the values one by one in the list using a for-loop like in the following. d = {k: [1,2,3]} > for v in d[k]: > print(v) No tutorial describes this, why? What is the Python explanation for this behaviour? Varuna --

Re: How would you name this dictionary?

2024-01-21 Thread Cameron Simpson via Python-list
On 21Jan2024 23:39, bagra...@live.com wrote: class NameMe(dict): def __missing__(self, key): return key I would need to know more about what it might be used for. What larger problem led you to writing a `dict` subclass with this particular `__missing__` implementation? -- https:/

How would you name this dictionary?

2024-01-21 Thread bagratte via Python-list
class NameMe(dict): def __missing__(self, key): return key -- https://mail.python.org/mailman/listinfo/python-list

Re: Dictionary order?

2022-08-01 Thread Weatherby,Gerard
I don’t see what is surprising. The interface for a dictionary never specified the ordering of the keys, so I would not be surprised to see it vary based on release, platform, values of keys inserted, number of items in the dictionary, etc. — Gerard Weatherby | Application Architect NMRbox

Re: Dictionary order?

2022-08-01 Thread Dan Stromberg
On Mon, Aug 1, 2022 at 4:42 PM Dan Stromberg wrote: > > > Yes, but I'm pretty sure that's been true for a LONG time. The hashes >> > for small integers have been themselves for as long as I can remember. >> > But the behaviour of the dictionary, when

Re: Dictionary order?

2022-08-01 Thread Dan Stromberg
, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19] > > > > > > Just sayin'. :-) > > > > Yes, but I'm pretty sure that's been true for a LONG time. The hashes > > for small integers have been themselves for as long as I can remember. > >

Re: Dictionary order?

2022-08-01 Thread 2QdxY4RzWzUUiLuE
uot;license" for more information. > > >>> [hash(x) for x in range(20)] > > [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19] > > > > Just sayin'. :-) > > Yes, but I'm pretty sure that's been true for a LONG time. Th

Re: Dictionary order?

2022-08-01 Thread Chris Angelico
1, 12, 13, 14, 15, 16, 17, 18, 19] > > Just sayin'. :-) Yes, but I'm pretty sure that's been true for a LONG time. The hashes for small integers have been themselves for as long as I can remember. But the behaviour of the dictionary, when fed such keys, is what's changed. ChrisA -- https://mail.python.org/mailman/listinfo/python-list

Re: Dictionary order?

2022-08-01 Thread 2QdxY4RzWzUUiLuE
On 2022-08-01 at 13:41:11 -0700, Dan Stromberg wrote: > keys = [5, 10, 15, 14, 9, 4, 1, 2, 8, 6, 7, 12, 11] > > dict_ = {} > for key in keys: > dict_[key] = 1 $ python Python 3.10.5 (main, Jun 6 2022, 18:49:26) [GCC 12.1.0] on linux Type "help", "copyright", "credits" or "license" for more

Re: Dictionary order?

2022-08-01 Thread Dan Stromberg
On Mon, Aug 1, 2022 at 1:41 PM Dan Stromberg wrote: > On 1.4 through 2.1 I got descending key order. I expected the keys to be > scattered, but they weren't. > I just noticed that 1.4 was ascending order too - so it was closer to 2.2 than 1.5. I guess that's kind of beside the point though - it

Re: Dictionary order?

2022-08-01 Thread Chris Angelico
On Tue, 2 Aug 2022 at 06:50, Skip Montanaro wrote: > > > > > So I decided to write a little test program to run on a variety of > > CPythons, to confirm what I was thinking. > > > > And instead I got a surprise. > > > > On 1.4 through 2.1 I got descending key order. I expected the keys to be > >

Re: Dictionary order?

2022-08-01 Thread Skip Montanaro
> > So I decided to write a little test program to run on a variety of > CPythons, to confirm what I was thinking. > > And instead I got a surprise. > > On 1.4 through 2.1 I got descending key order. I expected the keys to be > scattered, but they weren't. > > On 2.2 through 3.5 I got ascending ke

Dictionary order?

2022-08-01 Thread Dan Stromberg
Hi folks. I'm still porting some code from Python 2.7 to 3.10. As part of that, I saw a list being extended with a dict.values(), and thought perhaps it wasn't ordered as intended on Python 2.7, even though the problem would conceivably just disappear on 3.10. So I decided to write a little test

Re: Function to Print a nicely formatted Dictionary or List?

2022-06-09 Thread Dan Stromberg
On Thu, Jun 9, 2022 at 1:52 PM Michael F. Stemper wrote: > On 09/06/2022 12.52, Chris Angelico wrote: > > On Fri, 10 Jun 2022 at 03:44, Dave wrote: > > >> Before I write my own I wondering if anyone knows of a function that > will print a nicely formatted diction

Re: Function to Print a nicely formatted Dictionary or List?

2022-06-09 Thread Grant Edwards
On 2022-06-09, Dennis Lee Bieber wrote: > However, the Gmane list<>NNTP gateway server DOES make the tutor > list available to news readers (unfortunately, the comp.lang.python > <> list <> Gmane has been read-only since last fall (unless things > have changed recently) so I'm stuck with the spam

Re: Function to Print a nicely formatted Dictionary or List?

2022-06-09 Thread Cameron Simpson
On 09Jun2022 21:35, Dave wrote: >I quite like the format that JSON gives - thanks a lot! Note that JSON output is JavaScript notation, not Python. The `pprint` module (which has `pprint` and `pformat` functions) outputs Python notation. If you're just writing for human eyes, JSON is fine, thou

Re: Function to Print a nicely formatted Dictionary or List?

2022-06-09 Thread Michael F. Stemper
On 09/06/2022 12.52, Chris Angelico wrote: On Fri, 10 Jun 2022 at 03:44, Dave wrote: Before I write my own I wondering if anyone knows of a function that will print a nicely formatted dictionary? By nicely formatted I mean not all on one line! https://docs.python.org/3/library

Re: Function to Print a nicely formatted Dictionary or List?

2022-06-09 Thread Friedrich Rentsch
of a function that will print a nicely formatted dictionary? By nicely formatted I mean not all on one line! Cheers Dave -- https://mail.python.org/mailman/listinfo/python-list

Re: Function to Print a nicely formatted Dictionary or List?

2022-06-09 Thread Dave
Hi, I quite like the format that JSON gives - thanks a lot! Cheers Dave > On 9 Jun 2022, at 20:02, Stefan Ram wrote: > > Since nicety is in the eyes of the beholder, I would not > hesitate to write a custom function in this case. Python > has the standard modules "pprint" and "json". > >

Re: Function to Print a nicely formatted Dictionary or List?

2022-06-09 Thread Mats Wichmann
On 6/9/22 11:52, Chris Angelico wrote: > On Fri, 10 Jun 2022 at 03:44, Dave wrote: >> >> Hi, >> >> Before I write my own I wondering if anyone knows of a function that will >> print a nicely formatted dictionary? >> >> By nicely format

Re: Function to Print a nicely formatted Dictionary or List?

2022-06-09 Thread Avi Gross via Python-list
pprint may have what you want in pprint.pprint()  but you can write a function for yourself that takes a   dictionary and loops through items and prints them one per line and, if you feel like it, also prints how many items there are and your own custom touches such as doing them alphabetically

Re: Function to Print a nicely formatted Dictionary or List?

2022-06-09 Thread Chris Angelico
On Fri, 10 Jun 2022 at 03:44, Dave wrote: > > Hi, > > Before I write my own I wondering if anyone knows of a function that will > print a nicely formatted dictionary? > > By nicely formatted I mean not all on one line! > https://docs.python.org/3/library/pprint.html f

Re: Function to Print a nicely formatted Dictionary or List?

2022-06-09 Thread MRAB
On 2022-06-09 11:43, Dave wrote: Hi, Before I write my own I wondering if anyone knows of a function that will print a nicely formatted dictionary? By nicely formatted I mean not all on one line! It's called "pretty-printing". Have a look at the 'pprint' module. -

Re: Function to Print a nicely formatted Dictionary or List?

2022-06-09 Thread Larry Martell
On Thu, Jun 9, 2022 at 11:44 AM Dave wrote: > > Hi, > > Before I write my own I wondering if anyone knows of a function that will > print a nicely formatted dictionary? > > By nicely formatted I mean not all on one line! >>> import json >>> d = {&#x

Function to Print a nicely formatted Dictionary or List?

2022-06-09 Thread Dave
Hi, Before I write my own I wondering if anyone knows of a function that will print a nicely formatted dictionary? By nicely formatted I mean not all on one line! Cheers Dave -- https://mail.python.org/mailman/listinfo/python-list

Re: C API - How to return a Dictionary as a Dictionary type

2022-02-14 Thread Jen Kris via Python-list
Yes, that works.  This is my first day with C API dictionaries.  Now that you've explained it, it makes perfect sense.  Thanks much.  Jen Feb 14, 2022, 17:24 by ros...@gmail.com: > On Tue, 15 Feb 2022 at 12:07, Jen Kris via Python-list > wrote: > >> >> I created a

Re: C API - How to return a Dictionary as a Dictionary type

2022-02-14 Thread Chris Angelico
On Tue, 15 Feb 2022 at 12:07, Jen Kris via Python-list wrote: > > I created a dictionary with the Python C API and assigned two keys and values: > > PyObject* this_dict = PyDict_New(); > const char *key = "key1"; > char *val = "data_01"; > PyObject* v

C API - How to return a Dictionary as a Dictionary type

2022-02-14 Thread Jen Kris via Python-list
I created a dictionary with the Python C API and assigned two keys and values: PyObject* this_dict = PyDict_New();  const char *key = "key1"; char *val = "data_01";  PyObject* val_p = PyUnicode_FromString(val);  int r = PyDict_SetItemString(this_dict, key, val_p);  // Add

Re: Remove duplicate values from dictionary without removing key

2021-01-08 Thread Louis Krupp
On 1/8/2021 2:56 AM, Umar Draz wrote: I want to replace duplicate values with empty "" in my dictionary. Here is my original dictionary. items = [ {'client': 'xyz', 'name': "Ilir Meta", 'rating': 0.06, 'total': 100

Re: Remove duplicate values from dictionary without removing key

2021-01-08 Thread MRAB
On 2021-01-08 09:56, Umar Draz wrote: I want to replace duplicate values with empty "" in my dictionary. Here is my original dictionary. items = [ {'client': 'xyz', 'name': "Ilir Meta", 'rating': 0.06, 'total': 100

Remove duplicate values from dictionary without removing key

2021-01-08 Thread Umar Draz
I want to replace duplicate values with empty "" in my dictionary. Here is my original dictionary. items = [ {'client': 'xyz', 'name': "Ilir Meta", 'rating': 0.06, 'total': 100}, {'client': 'xyz'

Re: How to handle a dictionary value that is a list

2020-10-02 Thread Python
On Fri, Oct 02, 2020 at 09:25:58PM +0100, Shaozhong SHI wrote: > Hi, All, > > I was trying to handle the value of "personRoles" in a part of json > dictionary. > > Can anyone tell me various ways to handle this? I'm not sure if I understand what you're a

How to handle a dictionary value that is a list

2020-10-02 Thread Shaozhong SHI
Hi, All, I was trying to handle the value of "personRoles" in a part of json dictionary. Can anyone tell me various ways to handle this? Regards, Shao "regulatedActivities": [ { "name": "Accommodation for persons who require nursing or

Re: "dictionary changed size during iteration" error in Python 3 but not in Python 2

2020-08-23 Thread Peter Otten
ormation. >>> d = dict(a=1) >>> for k in d: del d["a"] ... Traceback (most recent call last): File "", line 1, in RuntimeError: dictionary changed size during iteration If you look at the 2.7 mailbox code you'll find that the iteritems() method delegate

Re: "dictionary changed size during iteration" error in Python 3 but not in Python 2

2020-08-23 Thread Chris Green
produces > >the error:- > > > >RuntimeError: dictionary changed size during iteration > > > >I can sort of see why I'm getting the error but have two questions: > > > >1 - Why doesn't it error in Python 2? > > The dict internal impl

Re: "dictionary changed size during iteration" error in Python 3 but not in Python 2

2020-08-23 Thread Cameron Simpson
On 23Aug2020 10:00, Chris Green wrote: >I have a (fairly) simple little program that removes old mail messages >from my junk folder. I have just tried to upgrade it from Python 2 to >Python 3 and now, when it finds any message[s] to delete it produces >the error:- > >Runtim

"dictionary changed size during iteration" error in Python 3 but not in Python 2

2020-08-23 Thread Chris Green
I have a (fairly) simple little program that removes old mail messages from my junk folder. I have just tried to upgrade it from Python 2 to Python 3 and now, when it finds any message[s] to delete it produces the error:- RuntimeError: dictionary changed size during iteration I can sort of

Re: When creating a nested dictionary of dataframes, how can I name a dictionary based on a list name of the dataframe?

2020-06-07 Thread Aaron
Thank you for the help! Based on your response/recommendation, I am thinking that my entire approach to solving my problem is very poor. I am trying to pull slices from a dataframe, store them in a nested dictionary, retrieve them, perform calculations, store the results in the same nested

Re: When creating a nested dictionary of dataframes, how can I name a dictionary based on a list name of the dataframe?

2020-06-07 Thread Peter Otten
Aaron wrote: > When creating a nested dictionary of dataframes, how can I name a > dictionary based on a list name of the dataframe? > > Given the following: > > # START CODE > import pandas as pd > > cars = {'Brand': ['Honda Civic',&#x

When creating a nested dictionary of dataframes, how can I name a dictionary based on a list name of the dataframe?

2020-06-06 Thread Aaron
When creating a nested dictionary of dataframes, how can I name a dictionary based on a list name of the dataframe? Given the following: # START CODE import pandas as pd cars = {'Brand': ['Honda Civic','Toyota Corolla'], 'Price': [22000,2500

Re: how to remove duplicates dict from the list of dictionary based on one of the elements is duplicate in list of dict

2019-11-16 Thread Cameron Simpson
On 17Nov2019 12:26, Iranna Mathapati wrote: How to remove duplicates dict from the list of dictionary based on one of the duplicate elements in the dictionary, l = [{"component":"software", "version":"1.2" }, {"component":"

how to remove duplicates dict from the list of dictionary based on one of the elements is duplicate in list of dict

2019-11-16 Thread Iranna Mathapati
Hi, How to remove duplicates dict from the list of dictionary based on one of the duplicate elements in the dictionary, l = [{"component":"software", "version":"1.2" }, {"component":"hardware", "version":"2.2"

Re: How do you insert an item into a dictionary (in python 3.7.2)?

2019-06-29 Thread Michael Torrie
On 06/28/2019 09:06 AM, CrazyVideoGamez wrote: > How do you insert an item into a dictionary? For example, I make a dictionary > called "dictionary". > > dictionary = {1: 'value1', 2: 'value3'} > > What if I wanted to add a value2 in the m

Re: How do you insert an item into a dictionary (in python 3.7.2)?

2019-06-28 Thread Chris Angelico
On Sat, Jun 29, 2019 at 1:51 AM Jon Ribbens via Python-list wrote: > > On 2019-06-28, Larry Martell wrote: > > On Fri, Jun 28, 2019 at 11:10 AM CrazyVideoGamez > > wrote: > >> > >> How do you insert an item into a dictionary? For example, I make a

Re: How do you insert an item into a dictionary (in python 3.7.2)?

2019-06-28 Thread Jon Ribbens via Python-list
On 2019-06-28, Larry Martell wrote: > On Fri, Jun 28, 2019 at 11:10 AM CrazyVideoGamez > wrote: >> >> How do you insert an item into a dictionary? For example, I make a >> dictionary called "dictionary". >> >> dictionary = {1: 'value1', 2:

Re: How do you insert an item into a dictionary (in python 3.7.2)?

2019-06-28 Thread Larry Martell
On Fri, Jun 28, 2019 at 11:10 AM CrazyVideoGamez wrote: > > How do you insert an item into a dictionary? For example, I make a dictionary > called "dictionary". > > dictionary = {1: 'value1', 2: 'value3'} > > What if I wanted to add a value2

Re: How do you insert an item into a dictionary (in python 3.7.2)?

2019-06-28 Thread Calvin Spealman
You simply assign to the key, like so: dictionary[3] = 'value2' But it isn't clear what you mean by "in the middle". On Fri, Jun 28, 2019 at 11:10 AM CrazyVideoGamez wrote: > How do you insert an item into a dictionary? For example, I make a > dictionary call

How do you insert an item into a dictionary (in python 3.7.2)?

2019-06-28 Thread CrazyVideoGamez
How do you insert an item into a dictionary? For example, I make a dictionary called "dictionary". dictionary = {1: 'value1', 2: 'value3'} What if I wanted to add a value2 in the middle of value1 and value3? -- https://mail.python.org/mailman/listinfo/python-list

Re: Dictionary

2019-02-27 Thread Peter Otten
Phu Sam wrote: > The condition 'if not fibs.get(n):' will not work because > n = 0 > fibs.get(0) is 0 so not 0 is 1 > > Here is the modified code that works: > > fibs={0:0,1:1} > def rfib(n): > if n == 0 or n == 1: > return fibs[n] > else: > fibs[n]=rfib(n-2

Re: Dictionary

2019-02-27 Thread Phu Sam
The condition 'if not fibs.get(n):' will not work because n = 0 fibs.get(0) is 0 so not 0 is 1 Here is the modified code that works: fibs={0:0,1:1} def rfib(n): if n == 0 or n == 1: return fibs[n] else: fibs[n]=rfib(n-2)+rfib(n-1) return fibs[n] >>>

Re: Dictionary

2019-02-25 Thread ast
Le 24/02/2019 à 05:21, Himanshu Yadav a écrit : fibs={0:0,1:1} def rfib(n): global fibs if not fibs.get(n): fibs[n]=rfib(n-2)+rfib(n-1) return fibs[n] Why it is gives error?? Nothing to do with the malfunction, but you dont need to define fibs as globa

Re: Dictionary

2019-02-24 Thread MRAB
On 2019-02-24 04:21, Himanshu Yadav wrote: fibs={0:0,1:1} def rfib(n): global fibs if not fibs.get(n): fibs[n]=rfib(n-2)+rfib(n-1) return fibs[n] Why it is gives error?? What error? -- https://mail.python.org/mailman/listinfo/python-list

Re: Dictionary

2019-02-24 Thread Peter Otten
Himanshu Yadav wrote: > fibs={0:0,1:1} > def rfib(n): > global fibs >if not fibs.get(n): > fibs[n]=rfib(n-2)+rfib(n-1) > return fibs[n] Please use cut and paste to make sure you are not introducing new errors like the inconsistent indentation above. > Why

Dictionary

2019-02-24 Thread Himanshu Yadav
fibs={0:0,1:1} def rfib(n): global fibs if not fibs.get(n): fibs[n]=rfib(n-2)+rfib(n-1) return fibs[n] Why it is gives error?? -- https://mail.python.org/mailman/listinfo/python-list

Re: Ask for help about class variable scope (Re: Why doesn't a dictionary work in classes?)

2018-12-28 Thread jfong
jf...@ms4.hinet.net於 2018年12月28日星期五 UTC+8下午4時04分07秒寫道: > eryk sun at 2018/12/27 UTC+8 PM 6:58:33 wrote: > > On 12/27/18, jf...@ms4.hinet.net wrote: > > > > > > I still don't get it. When I change it to using list comprehension, the > > > problem is still there. (it now has no late-binding variable

Re: Ask for help about class variable scope (Re: Why doesn't a dictionary work in classes?)

2018-12-28 Thread jfong
eryk sun at 2018/12/27 UTC+8 PM 6:58:33 wrote: > On 12/27/18, jf...@ms4.hinet.net wrote: > > > > I still don't get it. When I change it to using list comprehension, the > > problem is still there. (it now has no late-binding variable, right? :-) > > > class Too: > > ... XS = [15, 15, 15,

Re: Ask for help about class variable scope (Re: Why doesn't a dictionary work in classes?)

2018-12-27 Thread Chris Angelico
On Fri, Dec 28, 2018 at 8:47 AM eryk sun wrote: > > On 12/27/18, Chris Angelico wrote: > > > > Class scope is special, and a generator expression within that class > > scope is special too. There have been proposals to make these kinds of > > things less special, but the most important thing to r

Re: Ask for help about class variable scope (Re: Why doesn't a dictionary work in classes?)

2018-12-27 Thread eryk sun
On 12/27/18, Chris Angelico wrote: > > Class scope is special, and a generator expression within that class > scope is special too. There have been proposals to make these kinds of > things less special, but the most important thing to remember is that > when you create a generator expression, it

Re: Ask for help about class variable scope (Re: Why doesn't a dictionary work in classes?)

2018-12-27 Thread Chris Angelico
On Fri, Dec 28, 2018 at 3:31 AM Ian Kelly wrote: > > On Wed, Dec 26, 2018 at 11:21 PM Chris Angelico wrote: > > > > On Thu, Dec 27, 2018 at 1:56 PM wrote: > > > > > > I saw the code below at stackoverflow. I have a little idea about the > > > scope of a class, and list comprehension and generat

Re: Ask for help about class variable scope (Re: Why doesn't a dictionary work in classes?)

2018-12-27 Thread Ian Kelly
On Wed, Dec 26, 2018 at 11:21 PM Chris Angelico wrote: > > On Thu, Dec 27, 2018 at 1:56 PM wrote: > > > > I saw the code below at stackoverflow. I have a little idea about the scope > > of a class, and list comprehension and generator expressions, but still > > can't figure out why Z4 works and

Re: Ask for help about class variable scope (Re: Why doesn't a dictionary work in classes?)

2018-12-27 Thread eryk sun
On 12/27/18, jf...@ms4.hinet.net wrote: > > I still don't get it. When I change it to using list comprehension, the > problem is still there. (it now has no late-binding variable, right? :-) > class Too: > ... XS = [15, 15, 15, 15] > ... Z4 = [val for val in XS] > ... Z5 = [XS[0]

Re: Ask for help about class variable scope (Re: Why doesn't a dictionary work in classes?)

2018-12-27 Thread Terry Reedy
On 12/26/2018 9:53 PM, jf...@ms4.hinet.net wrote: I saw the code below at stackoverflow. I have a little idea about the scope of a class, and list comprehension and generator expressions, but still can't figure out why Z4 works and Z5 not. Can someone explain it? (in a not-too-complicated way:

Re: Ask for help about class variable scope (Re: Why doesn't a dictionary work in classes?)

2018-12-27 Thread jfong
eryk sun於 2018年12月27日星期四 UTC+8下午2時31分58秒寫道: > On 12/26/18, jf...@ms4.hinet.net wrote: > > I saw the code below at stackoverflow. I have a little idea about the scope > > of a class, and list comprehension and generator expressions, but still > > can't figure out why Z4 works and Z5 not. Can someon

Re: Ask for help about class variable scope (Re: Why doesn't a dictionary work in classes?)

2018-12-26 Thread eryk sun
On 12/26/18, jf...@ms4.hinet.net wrote: > I saw the code below at stackoverflow. I have a little idea about the scope > of a class, and list comprehension and generator expressions, but still > can't figure out why Z4 works and Z5 not. Can someone explain it? (in a > not-too-complicated way:-) > >

Re: Ask for help about class variable scope (Re: Why doesn't a dictionary work in classes?)

2018-12-26 Thread Chris Angelico
On Thu, Dec 27, 2018 at 1:56 PM wrote: > > I saw the code below at stackoverflow. I have a little idea about the scope > of a class, and list comprehension and generator expressions, but still can't > figure out why Z4 works and Z5 not. Can someone explain it? (in a > not-too-complicated way:-)

Re: Ask for help about class variable scope (Re: Why doesn't a dictionary work in classes?)

2018-12-26 Thread Abdur-Rahmaan Janhangeer
greetings, 1) Z4 = sum(val for val in XS) is same as Z4 = sum(XS) 2) class Foo() can also ne written as class Foo: 3) in Foo.x you are using the class just to assoxiate some variables with a name. what is the purpose of tge script / what are you trying to do? Abdur-Rahmaan Janhangeer htt

Ask for help about class variable scope (Re: Why doesn't a dictionary work in classes?)

2018-12-26 Thread jfong
I saw the code below at stackoverflow. I have a little idea about the scope of a class, and list comprehension and generator expressions, but still can't figure out why Z4 works and Z5 not. Can someone explain it? (in a not-too-complicated way:-) class Foo(): XS = [15, 15, 15, 15] Z4 =

Re: Why doesn't a dictionary work in classes?

2018-12-25 Thread אורי
, sorry about that. https://stackoverflow.com/questions/53924235/why-doesnt-a-dictionary-work-in-classes By the way, I remember having a problem in the past when writing a list comprehension which uses a variable name that was also used outside the list comprehension. for example "[i * 2 for i in

Re: Why doesn't a dictionary work in classes?

2018-12-25 Thread Peter Otten
אורי wrote: > Why does this not work: > > class User(ValidateUserPasswordMixin, PermissionsMixin, Entity, > AbstractBaseUser): > GENDER_VALID_VALUES = [choice[0] for choice in GENDER_CHOICES] > GENDERS_DICT = {GENDER_FEMALE: GENDER_FEMALE_STRING, GENDER_MALE: > ALL_GENDERS = [GENDER

Re: Why doesn't a dictionary work in classes?

2018-12-25 Thread eryk sun
On 12/25/18, אורי wrote: > > ALL_GENDERS = [GENDERS_DICT[gender] for gender in GENDER_VALID_VALUES] > > (it throws an exception: `NameError: name 'GENDERS_DICT' is not defined`) Python 3 comprehensions have their own scope. This prevents the side effect of defining the comprehension's loop variab

RE: Why doesn't a dictionary work in classes?

2018-12-25 Thread Avi Gross
emale") and the other two tuples something similar? You already declared similar (but all lowercase) strings above. So what is this odd notation for: _("Female") If that section fails, then GENDER_CHOICES does not get created. Did you not get an error message for that? Bu

Re: Why doesn't a dictionary work in classes?

2018-12-25 Thread Terry Reedy
On 12/25/2018 7:45 AM, אורי wrote: Why does this not work: class User(ValidateUserPasswordMixin, PermissionsMixin, Entity, AbstractBaseUser): GENDER_UNKNOWN = 0 > ... Indent needed here. I presume you used tabs, which disappeared in transmission, but I cannot cut and paste to run this. -

Why doesn't a dictionary work in classes?

2018-12-25 Thread אורי
Why does this not work: class User(ValidateUserPasswordMixin, PermissionsMixin, Entity, AbstractBaseUser): GENDER_UNKNOWN = 0 GENDER_FEMALE = 1 GENDER_MALE = 2 GENDER_OTHER = 3 GENDER_MAX_VALUE_PLUS_ONE = 4 GENDER_FEMALE_STRING = 'female' GENDER_MALE_STRING = 'male' GENDER_OTHER_STRING = 'other'

  1   2   3   4   5   6   7   8   9   10   >