Re: Python Class Best Practice
Rod Person wrote: > I've been doing python programming for about 2 years as a hobby and now > I'm finally able to use it at work in an enterprise environment. Since > I will be creating the base classes and libraries I wondering which why > is consider best when creating python classes: > > 1: > class Foo(object): > member1='' > member2=0 > > def __init__(self,member1='',member2=0): > self.member1 = member1 > self.member2 = member2 > > 2: > class Foo(object): > def __init__(self,member1='',member2=0): > self.member1 = member1 > self.member2 = member2 > > Both examples will store values for member1 and member2 in every instance. Any code that accesses self.member1 (or 2) will get the value stored in the instance. Example 1 which also creates two *class* members of the same name won't affect the conclusion of the previous paragraph. The two values in the class will be shadowed by each instances members of the same name. But now I need to ask, what did you expect to happen here? * If you thought those two extra assignments in example 1 effected the execution or storage in the __init__ (or any other method), you were mistaken. * If you really wanted class members (i.e., values shared by ALL instances), then they really shouldn't have the same name as instance members. You would surely confuse them at some point. * If you *really* wanted two class members *AND* two instance members with the same names, (WHY???) then example 1 will do so, but you'll have to access the instance members as self.member1 and the class members as Foo.member1. Gary Herron -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Class Best Practice
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 On Tue, 04 Dec 2007 15:51:18 -0800 Gary Herron <[EMAIL PROTECTED]> wrote: > Rod Person wrote: > > > > 1: > > class Foo(object): > > member1='' > > member2=0 > > > > def __init__(self,member1='',member2=0): > > self.member1 = member1 > > self.member2 = member2 > > > > 2: > > class Foo(object): > > def __init__(self,member1='',member2=0): > > self.member1 = member1 > > self.member2 = member2 > > > > > Both examples will store values for member1 and member2 in every > instance. Any code that accesses self.member1 (or 2) will get the > value stored in the instance. > > Example 1 which also creates two *class* members of the same name > won't affect the conclusion of the previous paragraph. The two > values in the class will be shadowed by each instances members of the > same name. > > But now I need to ask, what did you expect to happen here? Well I guess I should say that I'm coming to Python from Delphi, so I am used to creating classes like in #1, but I'm used to having to declare a scope for each member. Not having a scope declaration is really throwing me some. > > * If you thought those two extra assignments in example 1 effected > the execution or storage in the __init__ (or any other method), you > were mistaken. > Yes, this is what I thought. I thought that declaring member1='' would just assign the a default value on creation of the class (as in Delphi). I probably shouldn't have give the parameters in the __init__ a default value in example 1. > Gary Herron Thanks. - -- Rod http://roddierod.homeunix.net:8080 -BEGIN PGP SIGNATURE- Version: GnuPG v1.4.7 (FreeBSD) iD8DBQFHVeuucZAIaGStcnARAgEKAJ4/bJW9GSNTsmSgyOTokbCkEQFO7ACdErME 50Mgzge48M2z+nymifkByqo= =e3gV -END PGP SIGNATURE- -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Class Best Practice
On Dec 5, 4:18 am, Rod Person <[EMAIL PROTECTED]> wrote: > -BEGIN PGP SIGNED MESSAGE- > Hash: SHA1 > > I've been doing python programming for about 2 years as a hobby and now > I'm finally able to use it at work in an enterprise environment. Since > I will be creating the base classes and libraries I wondering which why > is consider best when creating python classes: > > 1: > class Foo(object): > member1='' > member2=0 > > def __init__(self,member1='',member2=0): > self.member1 = member1 > self.member2 = member2 > > 2: > class Foo(object): > def __init(self,member1='',member2=0): > self.member1 = member1 > self.member2 = member2 > > - -- > Rodhttp://roddierod.homeunix.net:8080 > -BEGIN PGP SIGNATURE- > Version: GnuPG v1.4.7 (FreeBSD) > > iD8DBQFHVeAscZAIaGStcnARAhRnAKCNFfjStOPGs/9tMI6bKuBQTPiJHQCdEMdY > OQPSSZlJWqkLxZvPwI2ctVs= > =CfvK > -END PGP SIGNATURE- It depends on what we try to achieve. The first definition gets me class defined with two class and instance variables of the names member1 and member2. Where as second definition gets class with only instance variables member1 and member2. Both the definitions would allow me, Foo().member1 and Foo().member2. Where as first definition would also allow me Foo.member1 and Foo.member2. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Class Best Practice
On Dec 5, 12:18 am, Rod Person <[EMAIL PROTECTED]> wrote: > -BEGIN PGP SIGNED MESSAGE- > Hash: SHA1 > > I've been doing python programming for about 2 years as a hobby and now > I'm finally able to use it at work in an enterprise environment. Since > I will be creating the base classes and libraries I wondering which why > is consider best when creating python classes: > > 1: > class Foo(object): > member1='' > member2=0 > > def __init__(self,member1='',member2=0): > self.member1 = member1 > self.member2 = member2 > > 2: > class Foo(object): > def __init(self,member1='',member2=0): > self.member1 = member1 > self.member2 = member2 > The short answer : if 2 works, then stick with it. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Class Best Practice
[EMAIL PROTECTED] a écrit : > On Dec 5, 12:18 am, Rod Person <[EMAIL PROTECTED]> wrote: >> -BEGIN PGP SIGNED MESSAGE- >> Hash: SHA1 >> >> I've been doing python programming for about 2 years as a hobby and now >> I'm finally able to use it at work in an enterprise environment. Since >> I will be creating the base classes and libraries I wondering which why >> is consider best when creating python classes: >> >> 1: >> class Foo(object): >> member1='' >> member2=0 >> >> def __init__(self,member1='',member2=0): >> self.member1 = member1 >> self.member2 = member2 >> >> 2: >> class Foo(object): >> def __init(self,member1='',member2=0): >> self.member1 = member1 >> self.member2 = member2 >> > > > The short answer : if 2 works, then stick with it. The yet-even-shorter-answer: 2 !-) -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Class Best Practice
On 4 Dec, 23:18, Rod Person <[EMAIL PROTECTED]> wrote: > -BEGIN PGP SIGNED MESSAGE- > Hash: SHA1 > > I've been doing python programming for about 2 years as a hobby and now > I'm finally able to use it at work in an enterprise environment. Since > I will be creating the base classes and libraries I wondering which why > is consider best when creating python classes: > > 1: > class Foo(object): > member1='' > member2=0 > > def __init__(self,member1='',member2=0): > self.member1 = member1 > self.member2 = member2 > > 2: > class Foo(object): > def __init(self,member1='',member2=0): > self.member1 = member1 > self.member2 = member2 > Don't forget to call the base class __init__ function -- http://mail.python.org/mailman/listinfo/python-list