Python string with character exchange

2019-07-14 Thread Matt Zand
I am new to Python. I am trying to solve below Python question:

Given a string, return a new string where the first and last chars have
been exchanged.


-- 
Cheers,

Matt Zand
Cell: 202-420-9192
Work: 240-200-6131
High School Technology Services 
DC Web Makers 
Coding Bootcamps 
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python string with character exchange

2019-07-14 Thread Reto
On Sun, Jul 14, 2019 at 12:20:56PM -0400, Matt Zand wrote:
> Given a string, return a new string where the first and last chars have
> been exchanged.

This sounds awfully like a homework question.
What did you try?
What concepts are you missing?

Did you already look into slicing / getting element from a list?
That will be what you need in the end.

You can easily get a character from a string like this

```
a = "some random string"
print(a[0])
```

strings are immutable in python, meaning you will need to create a new string.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How Do You Replace Variables With Their Values?

2019-07-14 Thread Ian Kelly
On Thu, Jul 11, 2019 at 11:10 PM Chris Angelico  wrote:
>
> On Fri, Jul 12, 2019 at 2:30 PM Aldwin Pollefeyt
>  wrote:
> >
> > Wow, I'm so sorry I answered on the question : "How do you replace a
> > variable with its value". For what i understood with the example values,
> > CrazyVideoGamez wants 3 variables named like the meal-names in
dictionary.
> > Yes, it's not secure unless you work with your own dataset (just like
> > sending your own created commands with set=True in subprocess). Yes
there
> > might be better solutions for the real problem. But maybe the user
really
> > has a purpose for it, in a secure environment with own datatset, it's a
> > valid answer for "How do you replace a variable with its value".
> >
>
> What you gave was dangerous advice, and yes, there IS a better
> solution - and an easier one. If you want to create variables
> dynamically, then just create them!
>
> for meal, parts in dinner.items():
> globals()[meal.replace(' ','_')] = dinner[meal]
>
> Python has a rich set of metaprogramming tools. Don't just always
> reach for exec and caveat it with "it's okay if you trust everything".

To be fair, if dinner is untrusted then this new version is still unsafe.
You've just allowed it to shadow any global or built-in it wants to.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How Do You Replace Variables With Their Values?

2019-07-14 Thread Chris Angelico
On Mon, Jul 15, 2019 at 5:45 AM Ian Kelly  wrote:
>
> On Thu, Jul 11, 2019 at 11:10 PM Chris Angelico  wrote:
> >
> > On Fri, Jul 12, 2019 at 2:30 PM Aldwin Pollefeyt
> >  wrote:
> > >
> > > Wow, I'm so sorry I answered on the question : "How do you replace a
> > > variable with its value". For what i understood with the example values,
> > > CrazyVideoGamez wants 3 variables named like the meal-names in
> dictionary.
> > > Yes, it's not secure unless you work with your own dataset (just like
> > > sending your own created commands with set=True in subprocess). Yes
> there
> > > might be better solutions for the real problem. But maybe the user
> really
> > > has a purpose for it, in a secure environment with own datatset, it's a
> > > valid answer for "How do you replace a variable with its value".
> > >
> >
> > What you gave was dangerous advice, and yes, there IS a better
> > solution - and an easier one. If you want to create variables
> > dynamically, then just create them!
> >
> > for meal, parts in dinner.items():
> > globals()[meal.replace(' ','_')] = dinner[meal]
> >
> > Python has a rich set of metaprogramming tools. Don't just always
> > reach for exec and caveat it with "it's okay if you trust everything".
>
> To be fair, if dinner is untrusted then this new version is still unsafe.
> You've just allowed it to shadow any global or built-in it wants to.

This is true, but that risk is in the original too. What I'd ACTUALLY
do, in this sort of situation, would be to make some sort of namespace
object, so I can write foo.Desert rather than using square bracket
notation; otherwise, though, I'd just stick with the original.

But if you want to unpack an object into a namespace, it's certainly
better to assign directly into the namespace than to eval.

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


Re: super or not super?

2019-07-14 Thread Paulo da Silva
Às 15:30 de 12/07/19, Thomas Jollans escreveu:
> On 12/07/2019 16.12, Paulo da Silva wrote:
>> Hi all!
>>
>> Is there any difference between using the base class name or super to
>> call __init__ from base class?
> 
> There is, when multiple inheritance is involved. super() can call
> different 'branches' of the inheritance tree if necessary.
> ...

Thank you Jollans. I forgot multiple inheritance. I never needed it in
python, so far.

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


Re: super or not super?

2019-07-14 Thread Paulo da Silva
Às 16:20 de 12/07/19, Rhodri James escreveu:
> On 12/07/2019 15:12, Paulo da Silva wrote:


> ...

> super() also has major advantages if you are stuck with multiple
> inheritance.  Raymond Hettinger has an excellent article on this here:
> https://rhettinger.wordpress.com/2011/05/26/super-considered-super/
> 

Thank you. I'll take a look at the suggestion.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: super or not super?

2019-07-14 Thread Chris Angelico
On Mon, Jul 15, 2019 at 10:51 AM Paulo da Silva
 wrote:
>
> Às 15:30 de 12/07/19, Thomas Jollans escreveu:
> > On 12/07/2019 16.12, Paulo da Silva wrote:
> >> Hi all!
> >>
> >> Is there any difference between using the base class name or super to
> >> call __init__ from base class?
> >
> > There is, when multiple inheritance is involved. super() can call
> > different 'branches' of the inheritance tree if necessary.
> > ...
>
> Thank you Jollans. I forgot multiple inheritance. I never needed it in
> python, so far.
>

Something to consider is that super() becomes useful even if someone
else uses MI involving your class. Using super() ensures that your
class will play nicely in someone else's hierarchy, not just your own.

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