property

2020-06-26 Thread ast

Hello,

I am wondering why this code is OK:

class Temperature:
def __init__(self):
self.celsius = 0

fahrenheit = property()

@fahrenheit.getter
def fahrenheit(self):
return 9/5*self.celsius +32

@fahrenheit.setter
def fahrenheit(self, value):
self.celsius = (value-32)*5/9


and this one is not:


class Temperature:
def __init__(self):
self.celsius = 0

fahrenheit = property()

@fahrenheit.getter
def f(self):
return 9/5*self.celsius +32

@fahrenheit.setter
def f(self, value):
self.celsius = (value-32)*5/9

In the second code, I just changed the names of the
decorated functions

Unforunately I was not able to find "property" source
code to understand how it works exactly

I wrote a my_property which makes the two previous codes
to work fine. This is why I don't understand why the
genuine property does't work when decorated functions
are named f rather than fahrenheit

Here it is:

class my_property:

def __init__(self, fget=None, fset=None, fdel=None):
self.fget = fget
self.fset = fset
self.fdel = fdel

def __get__(self, instance, owner):
if instance is None:
return self
return self.fget(instance)

def __set__(self, instance, value):
self.fset(instance, value)

def __delete__(self, instance):
self.fdel(instance)

def getter(self, fget):
self.fget = fget
return self

def setter(self, fset):
self.fset = fset
return self

def deleter(self, fdel):
self.fdel = fdel
return self
--
https://mail.python.org/mailman/listinfo/python-list


Re: Regarding error code 9

2020-06-26 Thread Igor Korot
Hi,

On Fri, Jun 26, 2020, 1:17 AM Manikandan S  wrote:

> Sir/Mam;
>   While installing matplotlib in python i face this issue that it shows
> error : erro no 9 please reply with some solution for this error i am
> beginner so please explain in detail about this error and guide me to fix
> this error.
>

What OS?
What python version?
Are you using pip to install?

Thank you.


>   Thank you
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: property

2020-06-26 Thread Barry Scott



> On 26 Jun 2020, at 08:02, ast  wrote:
> 
> Hello,
> 
> I am wondering why this code is OK:
> 
> class Temperature:
>def __init__(self):
>   self.celsius = 0
> 
>fahrenheit = property()
>   
>@fahrenheit.getter
>def fahrenheit(self):
>   return 9/5*self.celsius +32
>   
>@fahrenheit.setter
>def fahrenheit(self, value):
>   self.celsius = (value-32)*5/9
> 
> 
> and this one is not:
> 
> 
> class Temperature:
>def __init__(self):
>   self.celsius = 0
> 
>fahrenheit = property()
>   
>@fahrenheit.getter
>def f(self):
>   return 9/5*self.celsius +32
>   
>@fahrenheit.setter
>def f(self, value):
>   self.celsius = (value-32)*5/9
> 
> In the second code, I just changed the names of the
> decorated functions

It seems you have to change all the fahenheit to f for this to work.
You left 3.

> 
> Unforunately I was not able to find "property" source
> code to understand how it works exactly

I see these docs:

https://docs.python.org/3/library/functions.html?highlight=property#property 


which do not show your usage.

The code is here:

https://github.com/python/cpython/blob/master/Objects/descrobject.c 


It defines the PyProperty_Type that is the builtin property class.

Barry

> 
> I wrote a my_property which makes the two previous codes
> to work fine. This is why I don't understand why the
> genuine property does't work when decorated functions
> are named f rather than fahrenheit
> 
> Here it is:
> 
> class my_property:
> 
>def __init__(self, fget=None, fset=None, fdel=None):
>self.fget = fget
>self.fset = fset
>self.fdel = fdel
> 
>def __get__(self, instance, owner):
>if instance is None:
>return self
>return self.fget(instance)
> 
>def __set__(self, instance, value):
>self.fset(instance, value)
> 
>def __delete__(self, instance):
>self.fdel(instance)
> 
>def getter(self, fget):
>self.fget = fget
>return self
> 
>def setter(self, fset):
>self.fset = fset
>return self
> 
>def deleter(self, fdel):
>self.fdel = fdel
>return self
> -- 
> https://mail.python.org/mailman/listinfo/python-list 
> 

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


Re: I need to study Python

2020-06-26 Thread Joel Goldstick
On Thu, Jun 25, 2020 at 8:18 PM Igor Korot  wrote:
>
> Hi,
> Just sign-up for a local community college.
> Will be easier and u will keep motivation...
>
> Thank you.
>
>
> On Thu, Jun 25, 2020, 6:49 PM  wrote:
>
> > Hey, I'm a completely noob.
> > I want to learn python, how can i or where can i study python?
> > --
> > https://mail.python.org/mailman/listinfo/python-list
> >
> --
> https://mail.python.org/mailman/listinfo/python-list

start with python.org

-- 
Joel Goldstick
http://joelgoldstick.com/blog
http://cc-baseballstats.info/stats/birthdays
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: I need to study Python

2020-06-26 Thread J. Pic
What happens when people want to start a music instrument and start with
pure theory, scales and modes, it's that the instrument ends up in a
forgotten corner of the room because they are not having real fun with it.

I would like to suggest that you start with a pet project, you can even
start with a website for example in which case start with Django official
tutorial for example.

Have fun !
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: property

2020-06-26 Thread Peter Otten
Unknown wrote:

> Hello,
> 
> I am wondering why this code is OK:
> 
> class Temperature:
>  def __init__(self):
> self.celsius = 0
> 
>  fahrenheit = property()
> 
>  @fahrenheit.getter
>  def fahrenheit(self):
> return 9/5*self.celsius +32
> 
>  @fahrenheit.setter
>  def fahrenheit(self, value):
> self.celsius = (value-32)*5/9
> 
> 
> and this one is not:
> 
> 
> class Temperature:
>  def __init__(self):
> self.celsius = 0
> 
>  fahrenheit = property()
> 
>  @fahrenheit.getter
>  def f(self):
> return 9/5*self.celsius +32
> 
>  @fahrenheit.setter
>  def f(self, value):
> self.celsius = (value-32)*5/9
> 
> In the second code, I just changed the names of the
> decorated functions

>  @fahrenheit.getter
>  def f(self):

is syntactic sugar for

def f(self):
   ...
f = fahrenheit.getter(f)

> Unforunately I was not able to find "property" source
> code to understand how it works exactly

The getter/setter of the property builtin return a new property 

>>> p = property()
>>> q = p.getter(None)
>>> p is q
False

where you 

>  def getter(self, fget):
>  self.fget = fget
>  return self

modify the existing one.

 
> I wrote a my_property which makes the two previous codes
> to work fine. This is why I don't understand why the
> genuine property does't work when decorated functions
> are named f rather than fahrenheit
> 
> Here it is:
> 
> class my_property:
> 
>  def __init__(self, fget=None, fset=None, fdel=None):
>  self.fget = fget
>  self.fset = fset
>  self.fdel = fdel
> 
>  def __get__(self, instance, owner):
>  if instance is None:
>  return self
>  return self.fget(instance)
> 
>  def __set__(self, instance, value):
>  self.fset(instance, value)
> 
>  def __delete__(self, instance):
>  self.fdel(instance)
> 
>  def getter(self, fget):
>  self.fget = fget
>  return self
> 
>  def setter(self, fset):
>  self.fset = fset
>  return self
> 
>  def deleter(self, fdel):
>  self.fdel = fdel
>  return self


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


Re: I need to study Python

2020-06-26 Thread Wesley Peng

Pick a book like Core Python Programming, read it and do coding follow it.

regards.

[email protected] wrote:

Hey, I'm a completely noob.
I want to learn python, how can i or where can i study python?


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


Re: property

2020-06-26 Thread Dieter Maurer
ast wrote at 2020-6-26 09:02 +0200:
>Hello,
>
>I am wondering why this code is OK:
>
>class Temperature:
> def __init__(self):
>   self.celsius = 0
>
> fahrenheit = property()
>
> @fahrenheit.getter
> def fahrenheit(self):
>   return 9/5*self.celsius +32
>
> @fahrenheit.setter
> def fahrenheit(self, value):
>   self.celsius = (value-32)*5/9
>
>
>and this one is not:
>
>
>class Temperature:
> def __init__(self):
>   self.celsius = 0
>
> fahrenheit = property()
>
> @fahrenheit.getter
> def f(self):
>   return 9/5*self.celsius +32
>
> @fahrenheit.setter
> def f(self, value):
>   self.celsius = (value-32)*5/9

A decoration

  @dec...
  def f(...): ...

is a shorthand notation for

  def f(...): ...
  f = dec...(f)

In your first example, "fahrenheit" is defined as you expect.
In the second example, some property functions are bound to `f`.
It depends on implementation details, whether the final
`f` and/or `fahrenheit` has both getter/setter definitions.

Always use the same name for function and property (as you do in
your first class).

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