Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Class Members

I am just learning Python and I come from a C background so please let me know if I have any confusion / mix up between both.

Assume I have the following class:

class Node(object):     def __init__(self, element):         self.element = element         self.left = self.right = None      @classmethod     def tree(cls, element, left, right):         node = cls(element)         node.left = left         node.right = right         return node 

This is a class named Node, that overloads the constructor, to be able to handle different arguments if needed.

What is the difference between defining self.element in __init__ only (as shown above) as opposed to doing the following:

class Node(object):     element, left, right = None     def __init__(self, element):         self.element = element         self.left = self.right = None 

Isn't self.element in __init__ the same as the class's element variable defined? Wouldn't that just overwrite element from None to the element value passed into __init__?

like image 437
darksky Avatar asked Sep 13 '12 15:09

darksky


People also ask

What are the members of a Python class?

The attributes are data members (class variables and instance variables) and methods, accessed via dot notation. Class variable − A variable that is shared by all instances of a class. Class variables are defined within a class but outside any of the class's methods.

How do you see the members of a class in Python?

In Python, we use a dot (.) operator to access the members of a class.

What are the class members?

A class's members include all the members declared in the class, along with all members (except constructors and finalizers) declared in all classes in its inheritance hierarchy. Private members in base classes are inherited but are not accessible from derived classes. Fields are variables declared at class scope.

What is a Python member?

What is Membership Operators in Python? A Membership Operator in Python can be defined as being an operator that is used to validate the membership of a value. This operator is used to test memberships in variables such as strings, integers as well as tuples.


2 Answers

One is a class attribute, while the other is an instance attribute. They are different, but they are closely related to one another in ways that make them look the same at times.

It has to do with the way python looks up attributes. There's a hierarchy. In simple cases it might look like this:

instance -> Subclass -> Superclass -> object (built-in type) 

When you look for an attribute on instance like this...

`instance.val` 

...what actually happens is that first, Python looks for val in the instance itself. Then, if it doesn't find val, it looks in its class, Subclass. Then, if it doesn't find val there, it looks in the parent of Subclass, Superclass. This means that when you do this...

>>> class Foo():     foovar = 10       def __init__(self, val):         self.selfvar = val 

...all instances of Foo share foovar, but have their own distinct selfvars. Here's a simple, concrete example of how that works:

>>> f = Foo(5) >>> f.foovar 10 >>> Foo.foovar 10 

If we don't touch foovar, it's the same for both f and Foo. But if we change f.foovar...

>>> f.foovar = 5 >>> f.foovar 5 >>> Foo.foovar 10 

...we add an instance attribute that effectively masks the value of Foo.foovar. Now if we change Foo.foovar directly, it doesn't affect our foo instance:

>>> Foo.foovar = 7 >>> f.foovar 5 

But it does affect a new foo instance:

>>> Foo(5).foovar 7 

Also keep in mind that mutable objects add another layer of indirection (as mgilson reminded me). Here, f.foovar refers to the same object as Foo.foovar, so when you alter the object, the changes are propagated up the hierarchy:

>>> Foo.foovar = [1] >>> f = Foo(5) >>> f.foovar[0] = 99 >>> Foo.foovar [99] 
like image 182
senderle Avatar answered Sep 23 '22 17:09

senderle


In python it is possible to have class variables and instance variables of the same name. They are located separately in memory, and are accessed quite differently.

In your code:

class Node(object):     element, left, right = None     def __init__(self, element):         self.element = element         self.left = self.right = None 

The first set of variables (outside the __init__ function) are called class variables. These can be subsequently accessed using Node.element, etc. These are equivalent to static member variables in C++, and they are shared by all instances of the class.

The second set of variables (inside the __init__ function) are called instance variables. These are accessed via the self object, e.g. self.element, or by the instance name e.g. myNode.element outside of the class.

It is important to note that you have to use either the self.variable or Node.variable form to access either of these from within a member function. Just accessing variable will try to access a local variable called variable.

like image 30
Lee Netherton Avatar answered Sep 21 '22 17:09

Lee Netherton