difference between class and instance attributes

0 votes

what is the difference between the following codes?

1. class A(object):
    foo = 3   #set default value
2. class B(object):
    def __init__(self, foo=3):
        self.foo = foo
How will it affect the performance or space requirements while creating a lot of instances?
Sep 17, 2018 in Python by ana1504.k
• 5,910 points
20 views

2 answers to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

In Python, there is no need to compare to True or False. In the following code, the opposite of dest in smaller.keys() is (dest not in smaller.keys())

So it must be written as:

if(dest not in smaller.keys()):

answered Sep 17, 2018 by SDeb
• 9,380 points
0 votes

Apart from the performance, there is a significant semantic difference between the two codes. only one object is referred to in case of class attribute whereas there can be multiple objects referred to in case of instance-attribute-set-at-instantiation.

For example :

1.   class A: foo = [] 

     a, b = A(), A() 

     a.foo.append(3) 

      b.foo

2.  class A: 

     def __init__(self): self.foo = [] 

     a, b = A(), A() a.foo.append(3) 

      b.foo

answered Sep 17, 2018 by SDeb
• 9,380 points

Related Questions In Python

0 votes
1 answer

What's the difference in Qt between setVisible, setShown and show/hide

show() is just a convenience function for ...READ MORE

answered Apr 17, 2018 in Python by anonymous
530 views
0 votes
1 answer

What is the difference between list and tuple?

Lists are mutable(values can be changed) whereas ...READ MORE

answered May 4, 2018 in Python by aayushi
• 750 points
978 views
0 votes
1 answer

Difference between '==' and 'is'

'==' checks for the equality of the ...READ MORE

answered May 14, 2018 in Python by Nietzsche's daemon
• 4,260 points
12 views
0 votes
1 answer

Difference between pop and delete methods

I think you mean del() and not ...READ MORE

answered Jun 25, 2018 in Python by Hamartia's Mask
• 1,580 points
18 views
0 votes
1 answer

how can i count the items in a list?

suppose you have a list a = [0,1,2,3,4,5,6,7,8,9,10] now ...READ MORE

answered May 2 in Python by Mohammad
• 1,400 points
19 views
0 votes
1 answer

Difference between module and Package in Python

A module is basically a single file ...READ MORE

answered Nov 13, 2018 in Python by SDeb
• 9,380 points
66 views
0 votes
1 answer

difference between "*" and "**"

The "**" operator is used for 'power ...READ MORE

answered Nov 19, 2018 in Python by SDeb
• 9,380 points
15 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.