Python Class Best Practice

R

Rod Person

-----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


- --
Rod
http://roddierod.homeunix.net:8080
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (FreeBSD)

iD8DBQFHVeAscZAIaGStcnARAhRnAKCNFfjStOPGs/9tMI6bKuBQTPiJHQCdEMdY
OQPSSZlJWqkLxZvPwI2ctVs=
=CfvK
-----END PGP SIGNATURE-----
 
P

Pavan Mishra

-----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.
 
C

cptnwillard

-----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.
 
M

MarkE

-----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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top