Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #76796 > unrolled thread

Re: Why can not initialize the class?

Started byluofeiyu <elearn2014@gmail.com>
First post2014-08-22 22:58 +0800
Last post2014-08-25 16:33 +1000
Articles 6 — 5 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Why can not initialize the class? luofeiyu <elearn2014@gmail.com> - 2014-08-22 22:58 +0800
    Re: Why can not initialize the class? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-08-23 01:17 +1000
      Re: Why can not initialize the class? luofeiyu <elearn2014@gmail.com> - 2014-08-23 07:29 +0800
        Re: Why can not initialize the class? CHIN Dihedral <dihedral88888@gmail.com> - 2014-08-23 10:25 -0700
          Re: Why can not initialize the class? Michael Torrie <torriem@gmail.com> - 2014-08-23 20:03 -0600
      Re: Why can not initialize the class? alex23 <wuwei23@gmail.com> - 2014-08-25 16:33 +1000

#76796 — Re: Why can not initialize the class?

Fromluofeiyu <elearn2014@gmail.com>
Date2014-08-22 22:58 +0800
SubjectRe: Why can not initialize the class?
Message-ID<mailman.13293.1408719557.18130.python-list@python.org>
 >>> class Contact(object):
...     def __init__(self, first_name=None, last_name=None,
...                  display_name=None, email="haha@haha"):
...         self.first_name = first_name
...         self.last_name = last_name
...         self.display_name = display_name
...         self.email = email
...     def print_info(self):
...         print(self.display_name, "<" + self.email + ">"  )
...     def set_email(self, value):
...         print(value)
...         self._email = value
...     def get_email(self):
...         return self._email
...     email = property(get_email, set_email)
...
 >>>
 >>> contact = Contact()
haha@haha

why the value in `def set_email(self, value): `  is  haha@haha?
how haha@haha  is called to  value in `def set_email(self, value): `?
would you mind telling me the process?

>
>

[toc] | [next] | [standalone]


#76798

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2014-08-23 01:17 +1000
Message-ID<53f75f22$0$6512$c3e8da3$5496439d@news.astraweb.com>
In reply to#76796
Luofeiyu, you are getting stuck on basic questions. Before working with
advanced features like properties, you should learn the simply features.


luofeiyu wrote:

> >>> class Contact(object):
> ...     def __init__(self, first_name=None, last_name=None,
> ...                  display_name=None, email="haha@haha"):
> ...         self.first_name = first_name
> ...         self.last_name = last_name
> ...         self.display_name = display_name
> ...         self.email = email
> ...     def print_info(self):
> ...         print(self.display_name, "<" + self.email + ">"  )
> ...     def set_email(self, value):
> ...         print(value)
> ...         self._email = value
> ...     def get_email(self):
> ...         return self._email
> ...     email = property(get_email, set_email)
> ...
>  >>>
>  >>> contact = Contact()
> haha@haha
> 
> why the value in `def set_email(self, value): `  is  haha@haha?
> how haha@haha  is called to  value in `def set_email(self, value): `?
> would you mind telling me the process?

Instead of this complicated example, start with this simple example:

class Contact(object):
    def __init__(self, email="haha@haha"):
        self.email = email

contact = Contact()
print(contact.email)


Do you understand how contact.email gets set to "haha@haha"?


Now let's make it a bit more complicated:

class Contact(object):
    def __init__(self, email="haha@haha"):
        self.set_email(email)
    def set_email(self, value):
        self.email = value

contact = Contact()
print(contact.email)


Do you still understand how contact.email gets set to "haha@haha"?


One final version:

class Contact(object):
    def __init__(self, email="haha@haha"):
        self.email = email
    def _get_email(self):
        return self._the_secret_private_email
    def _set_email(self, value):
        self.self._the_secret_private_email = value
    email = property(_get_email, _set_email)

contact = Contact()
print(contact.email)


Now do you understand how contact.email gets set to "haha@haha"?



-- 
Steven

[toc] | [prev] | [next] | [standalone]


#76832

Fromluofeiyu <elearn2014@gmail.com>
Date2014-08-23 07:29 +0800
Message-ID<mailman.13318.1408750213.18130.python-list@python.org>
In reply to#76798
One final version:

class Contact(object):
     def __init__(self, email="haha@haha"):
         self.email = email
     def _get_email(self):
         return self._the_secret_private_email
     def _set_email(self, value):
         self.self._the_secret_private_email = value
     email = property(_get_email, _set_email)

contact = Contact()
print(contact.email)

There is a little mistake here. It is

self._the_secret_private_email = value

not

self.self._the_secret_private_email = value

think for your demo .The value in `def _set_email(self, value):` is the value of self.email .

[toc] | [prev] | [next] | [standalone]


#76883

FromCHIN Dihedral <dihedral88888@gmail.com>
Date2014-08-23 10:25 -0700
Message-ID<4d1aa42e-115b-446a-8ee5-dce92450cca2@googlegroups.com>
In reply to#76832
On Saturday, August 23, 2014 7:29:44 AM UTC+8, luofeiyu wrote:
> One final version:
> 
> 
> 
> class Contact(object):
> 
>      def __init__(self, email="haha@haha"):
> 
>          self.email = email
> 
>      def _get_email(self):
> 
>          return self._the_secret_private_email
> 
>      def _set_email(self, value):
> 
>          self.self._the_secret_private_email = value
> 
>      email = property(_get_email, _set_email)
> 
> 
> 
> contact = Contact()
> 
> print(contact.email)
> 
> 
> 
> There is a little mistake here. It is
> 
> 
> 
> self._the_secret_private_email = value
> 
> 
> 
> not
> 
> 
> 
> self.self._the_secret_private_email = value
> 
> 
> 
> think for your demo .The value in `def _set_email(self, value):` is the value of self.email .

Well, an object in python can add
properties in the run-time to evolve
steadily and stealthly.

Those unnessary set-get-C++ methods
are not very important in PYTHON.

[toc] | [prev] | [next] | [standalone]


#76919

FromMichael Torrie <torriem@gmail.com>
Date2014-08-23 20:03 -0600
Message-ID<mailman.13370.1408845806.18130.python-list@python.org>
In reply to#76883
On 08/23/2014 11:25 AM, CHIN Dihedral wrote:
> Well, an object in python can add
> properties in the run-time to evolve
> steadily and stealthly.
> 
> Those unnessary set-get-C++ methods
> are not very important in PYTHON.

That's the most coherent thing I've seen from Dihedral in years!


[toc] | [prev] | [next] | [standalone]


#76966

Fromalex23 <wuwei23@gmail.com>
Date2014-08-25 16:33 +1000
Message-ID<ltelc1$gjb$2@dont-email.me>
In reply to#76798
On 23/08/2014 1:17 AM, Steven D'Aprano wrote:
> Luofeiyu, you are getting stuck on basic questions. Before working with
> advanced features like properties, you should learn the simply features.

Has luofeiyu ever actually acknowledged any such comment or request 
people have made? I see we've given up on trying to get em to stop 
top-posting, so I'm guessing no.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web