Are Class variables mutable? [duplicate] - python

This question already has answers here:
Class (static) variables and methods
(27 answers)
What is the difference between class and instance attributes?
(5 answers)
Closed 5 years ago.
If I define a simple class
class someClass():
var = 1
x = someClass()
someClass.var = 2
This will make x.var equal 2. This is confusing to be because normally
something akin to this like:
a = 1
b = a
a = 2
will leave b intact as b==1. So why is this not the same with class variables? Where is the difference? Can call all class variables mutable?
In a way the class variables work more like assigning a list to a=[1] and doing a[0]=2.
Basically the problem is how is x.var acessing someClass.var it must be something different then is used when two variables are set equal in python. What is happening?

var is a static class variable of someClass.
When you reach out to get x.var, y.var or some_other_instance.var, you are accessing the same variable, not an instance derived one (as long as you didn't specifically assigned it to the instance as a property).

Related

What's the point of creating classes and self in python? [duplicate]

This question already has answers here:
When should I be using classes in Python?
(6 answers)
What is the purpose of the `self` parameter? Why is it needed?
(26 answers)
Closed 8 months ago.
What's the point of creating classes and self in python? Like:
class ThisIsClass(self):
def Func():
self.var+=1
ThisIsClass()
Is it necessary to use them?
It becomes necessary to pass self argument to the functions of your class. however, your code has several problems like the initialization of var variable. You need a constructor __init__ to create the variable, after that, you will be able to modify it:
class ThisIsClass:
def __init__(self, value=0):
self.var = value
def Func(self):
self.var+=1
c = ThisIsClass()
c.Func()
print(c.var)
In the above code, the class is instantiated as an object and stored in the variable c. You can assign an initial value to var by passing an argument to the object creation, like: c = ThisIsClass(89)
Output:
1
It has to do with the right structure of programming in python. No you dont need them 100% but if you want to have a nice and clean code I would propose you to get used of them. Also when the self is declared inside the brackets after the function name, that function can retrieve (and make available) the variable (self.variable) from and to wherever inside that class.

Why does the property builtin work in conjunction with classes but not outside of them? [duplicate]

This question already has answers here:
How does the #property decorator work in Python?
(15 answers)
How do Python properties work?
(4 answers)
Closed 4 years ago.
I can do
class Foo(object):
x = property(lambda _: 123)
f = Foo()
f.x
to get 123
However, if I try
p = property(lambda : 123)
p
I get
<property object at 0x108f2f3b8>
Now I understand that an member of a class instance is not the same thing as a regular variable but I'm not sure what exactly makes this behavior different.
Does the fact that you instantiate a class somehow do extra binding on property objects? Is it a special case or is it a behavior I can take advantage in other situations and extend? Related - are property objects useful outside of a class declaration? Or is it just for this specific case?

python class attributes: unexpected behaviour [duplicate]

This question already has answers here:
How to avoid having class data shared among instances?
(7 answers)
Closed 9 years ago.
i just have a puzzling question abou class attributes in python.
consider the following class below:
class A:
__lst = []
def add(self, str):
self.__lst.append(str)
print len(self.__lst)
i tried to make two instances x and y and i got this:
>>> x = A()
>>> x.add('aaa')
1
>>> x.add('bbb')
2
>>> y = A()
>>> y.add('aaa')
3
>>> y.add('bbb')
4
i was expecting that the instance of y will have a separate copy of the list attribute but it seems that the list just gets bigger even if you use another object to add elements to the list. it's simply weird.
can anybody please enlighten me on this matter?
thanks a lot in advance for your help. :-)
If you define an attribute inside of the class body then it will be a class attribute, and it will be shared by all instances. In your code self.__lst is going to be a reference to A.__lst.
To have a separate list for each attribute, define it as self.__lst inside of the __init__() function:
class A(object):
def __init__(self):
self.__lst = []
def add(self, s):
self.__lst.append(s)
print len(self.__lst)
In addition to the change referenced above, I also made some minor modifications so that your code follows some Python best practices: inheriting from object (new-style class) and not using str (or any other built-in name) as a variable name.
Variables declared inside a class but not by means of self are class-level properties (like your __lst). They are equivalent to Java's static. If you want your property to be unique for all instances, you need to declare them via self (i.e., self.__lst).

What's the difference between class attributes and data attributes in python [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Python: Difference between class and instance attributes
class a:
m = 1
def __init__(self):
self.n = 2
According to the book I am reading, m is called class attributes and n is called data attributes, but what's the difference between them?
It seems that the operations of them is nearly same. The only difference I can tell is one is in the function __init__ and another isn't.
This is essentially a duplicate of this question which has an example of the difference.
Python: Difference between class and instance attributes

How to get the name of attribute in python object? [duplicate]

This question already has answers here:
List attributes of an object [duplicate]
(18 answers)
Closed 4 years ago.
For example I have next python class
class Myclass():
a = int
b = int
Imagine that I don't know the name this class, so I need to get the names of attributes? ("a" and "b")
If you want all (including private) attributes, just
dir(Myclass)
Attributes starting with _ are private/internal, though. For example, even your simple Myclass will have a __module__ and an empty __doc__ attribute. To filter these out, use
filter(lambda aname: not aname.startswith('_'), dir(Myclass))

Categories

Resources