Re: convert a string to a variable

2018-05-25 Thread Ned Batchelder

On 5/25/18 9:52 AM, brucegoodst...@gmail.com wrote:

On Friday, May 25, 2018 at 8:06:31 AM UTC-4, Ned Batchelder wrote:

On 5/24/18 6:54 PM, bruceg113...@gmail.com wrote:

I am trying to convert a string to a variable.

I got cases 1 & 2 to work, but not cases 3 & 4.

The print statement in cases 3 & 4 reports the following:
  builtins.AttributeError: type object 'animal' has no attribute 'tiger'
  
I am stuck on creating variables that can be accessed as follows.

animal.tiger
self.animal.tiger

Any suggestions?



Usually when people want to turn strings into variables, the best answer
is to not make variables, but instead have a dictionary.

But I don't know what you are going to do with "animal.tiger", so I'm
not sure the best answer.  Can you say more about the whole problem?

--Ned.


Hi Ned,

I am writing a small interpreter just for fun.
My program reads and processes text from a file.
Good or bad, I prefer the variable syntax.


If your own code will never use these variable names, then all of your 
accesses, both reading and writing, will be through these awkward 
mechanisms, I think?  Wouldn't it be easier to use your own dictionary?


--Ned.
--
https://mail.python.org/mailman/listinfo/python-list


Re: convert a string to a variable

2018-05-25 Thread brucegoodstein
On Friday, May 25, 2018 at 8:06:31 AM UTC-4, Ned Batchelder wrote:
> On 5/24/18 6:54 PM, bruceg113...@gmail.com wrote:
> > I am trying to convert a string to a variable.
> >
> > I got cases 1 & 2 to work, but not cases 3 & 4.
> >
> > The print statement in cases 3 & 4 reports the following:
> >  builtins.AttributeError: type object 'animal' has no attribute 'tiger'
> >  
> > I am stuck on creating variables that can be accessed as follows.
> >animal.tiger
> >self.animal.tiger
> >
> > Any suggestions?
> >
> >
> 
> Usually when people want to turn strings into variables, the best answer 
> is to not make variables, but instead have a dictionary.
> 
> But I don't know what you are going to do with "animal.tiger", so I'm 
> not sure the best answer.  Can you say more about the whole problem?
> 
> --Ned.


Hi Ned,

I am writing a small interpreter just for fun.
My program reads and processes text from a file.
Good or bad, I prefer the variable syntax.

I got animal.tiger syntax to work. (thanks Dieter)
I am still trying to get self.animal.tiger syntax to work.

Thanks,
Bruce

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: convert a string to a variable

2018-05-25 Thread brucegoodstein
On Friday, May 25, 2018 at 1:56:14 AM UTC-4, dieter wrote:
> bruceg113...@gmail.com writes:
> 
> > I am trying to convert a string to a variable.
> >
> > I got cases 1 & 2 to work, but not cases 3 & 4.
> >
> > The print statement in cases 3 & 4 reports the following:
> > builtins.AttributeError: type object 'animal' has no attribute 'tiger'
> > 
> > I am stuck on creating variables that can be accessed as follows.
> >   animal.tiger
> >   self.animal.tiger
> >
> > Any suggestions?
> > ...
> > # Case 3: This does not work
> > indata = 'animal.tiger'
> > vars()[indata] = "Tigers, big and strong!"
> > print (animal.tiger)
> 
> In the expression "animal.tiger", the "variable" is "animal",
> not "animal.tiger". It is evaluated as follows:
> determine the object bound to "animal", access its attribute "tiger".
> Your error message tells you that the first step (object bound
> to "animal") has been successful, but the result lacks the
> attribute "tiger".
> 
> > #Case 4: This does not work
> > class animal():
> > def create (self, indata):
> > vars(self)[indata] = "Tigers, big and strong!"
> 
> Here you want to define the attribute "tiger" (I think),
> not "animal.tiger". Note that the "." in a Python expression
> (not a string) separates two individual steps: determine
> an object corresponding to the leftside to the "."; access
> the attribute corresponding to the name following the ".".
> > print (self.animal.tiger)
> >
> > tmp = animal()
> > tmp.create('animal.tiger')


Hi Dieter,

After reading your response, I picked up on the 'attribute' word.

Updated case 3: (Success)

indata = 'tiger'
setattr (animal, indata, "Tigers, big and strong!!!")
print (animal.tiger)
animal.tiger = "Lions are bigger and stronger"
print (animal.tiger)

Output is:
Tigers, big and strong!!!
Lions are bigger and stronger

I am still working on case 4 using setattr.

Thanks,
Bruce

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: convert a string to a variable

2018-05-25 Thread Ned Batchelder

On 5/24/18 6:54 PM, bruceg113...@gmail.com wrote:

I am trying to convert a string to a variable.

I got cases 1 & 2 to work, but not cases 3 & 4.

The print statement in cases 3 & 4 reports the following:
 builtins.AttributeError: type object 'animal' has no attribute 'tiger'
 
I am stuck on creating variables that can be accessed as follows.

   animal.tiger
   self.animal.tiger

Any suggestions?




Usually when people want to turn strings into variables, the best answer 
is to not make variables, but instead have a dictionary.


But I don't know what you are going to do with "animal.tiger", so I'm 
not sure the best answer.  Can you say more about the whole problem?


--Ned.
--
https://mail.python.org/mailman/listinfo/python-list


Re: convert a string to a variable

2018-05-24 Thread dieter
bruceg113...@gmail.com writes:

> I am trying to convert a string to a variable.
>
> I got cases 1 & 2 to work, but not cases 3 & 4.
>
> The print statement in cases 3 & 4 reports the following:
> builtins.AttributeError: type object 'animal' has no attribute 'tiger'
> 
> I am stuck on creating variables that can be accessed as follows.
>   animal.tiger
>   self.animal.tiger
>
> Any suggestions?
> ...
> # Case 3: This does not work
> indata = 'animal.tiger'
> vars()[indata] = "Tigers, big and strong!"
> print (animal.tiger)

In the expression "animal.tiger", the "variable" is "animal",
not "animal.tiger". It is evaluated as follows:
determine the object bound to "animal", access its attribute "tiger".
Your error message tells you that the first step (object bound
to "animal") has been successful, but the result lacks the
attribute "tiger".

> #Case 4: This does not work
> class animal():
> def create (self, indata):
> vars(self)[indata] = "Tigers, big and strong!"

Here you want to define the attribute "tiger" (I think),
not "animal.tiger". Note that the "." in a Python expression
(not a string) separates two individual steps: determine
an object corresponding to the leftside to the "."; access
the attribute corresponding to the name following the ".".
> print (self.animal.tiger)
>
> tmp = animal()
> tmp.create('animal.tiger')

-- 
https://mail.python.org/mailman/listinfo/python-list


convert a string to a variable

2018-05-24 Thread bruceg113355
I am trying to convert a string to a variable.

I got cases 1 & 2 to work, but not cases 3 & 4.

The print statement in cases 3 & 4 reports the following:
builtins.AttributeError: type object 'animal' has no attribute 'tiger'

I am stuck on creating variables that can be accessed as follows.
  animal.tiger
  self.animal.tiger

Any suggestions?

Thanks,
Bruce


# Tested on Python 3.6.5

# Case 1: This works
indata = 'animal_tiger'
vars()[indata] = "Tigers, big and strong!"
print (animal_tiger)


# Case 2: This works
class animal():
def create (self, indata):
vars(self)[indata] = "Tigers, big and strong!"
print (self.animal_tiger)

tmp = animal()
tmp.create('animal_tiger')


#

# Case 3: This does not work
indata = 'animal.tiger'
vars()[indata] = "Tigers, big and strong!"
print (animal.tiger)


#Case 4: This does not work
class animal():
def create (self, indata):
vars(self)[indata] = "Tigers, big and strong!"
print (self.animal.tiger)

tmp = animal()
tmp.create('animal.tiger')











-- 
https://mail.python.org/mailman/listinfo/python-list