Simple Question : about python class parenthesis - python

Look at the code below.
class A :
def __init__(self, a = "Hello") :
self.a = a
print(A().a) # 1
print(A.a) # 2
1 is not error
2 is error - AttributeError: type object 'A' has no attribute 'a'
What is the difference between the two results?

In your code A refers the the type of a class and also to its constructor/initialiser. A is called the class and when you construct an object of type A with the constructor you get an instance of that class.
A # Refers to the class A
A() # is an instance of class A
There is a difference between a class property and an instance property. Consider the following code:
class A:
propertyA = "hello"
def __init__(self, string="world"):
self.propertyB = string
In this snippet propertyA is a class property while propertyB is an instance property. Each instance of type A has its own propertyB and you must instantiate and object (an instance) first.
A.propertyA # Class property, does not need an instance
A().propertyB # instance property, needs an instance
In your code the constructor for A is the code written in the __init__. This code will be called when you type A(). Note that you specified a default value for the parameter a but if you don't you would call the constructor like this:
A("hello") # or:
A(a="hello")
Note that classes, instances and constructors are fundamentals of OOP (and by extension Python), you really should learn this, it avoids lots of basic errors.

You need to create an instance of the class first:
class A :
def __init__(self, a = "Hello") :
self.a = a
class_instance = A()
print(class_instance.a)
You can set the value of "a" when creating the instance by typing in the parenthesis:
class_instance = A("this is the value of a")
you can change the value after the creation like so:
class_instance.a = "New value of a"
A().a is creating an instance and returns the a value of the instance.
A.a cannot be executed because A is the Class name and doesn't have any attributes if you don't create an instance first

Related

Class Level Constructor for the Python Class

I understand that __init__() is the constructor for an instance of the class. It is called whenever a class is instantiated. There is also the concept of class variables -- variables that belong to the class and shared by all instances. For example:
class A(object):
a = 1
b = [] #a and b are shared by all instances of class A
But the problem is that sometimes it takes more code to initialize a and b than the one lines showing above. Therefore there is a need for 'class level constructor.' I wonder, though, if there is such a thing.
You can just do something like
class A(object):
# .......
var = input()
var += 10
# ecc.
A.b = var

Python: Find Instance of a class by value

i created much instances of a class. Then I want to find one instance by its name. But I get the error message TypeError: get() missing 1 required positional argument: 'value'.
class Test(object):
def __init__(self, value):
self.value = value
def get(self, value):
if self.value == value:
return self
else:
return None
test_obj = Test('foobar')
print(test_obj.value)
instance = Test.get('foobar')
if instance:
print(instance.value)
Re-reading your question again, I think all of us have missed the point so far. You wanted to check all instances of the class Test to see if an instance has the value 'foobar' (in this case, test_obj. Referencing this answer, you can modify your code like so:
class Test(object):
# class attribute to keep track of class instances
instances = []
def __init__(self, value):
self.value = value
Test.instances.append(self)
# class method to access the get method without any instance
#classmethod
def get(cls, value):
return [inst for inst in cls.instances if inst.value == value]
You can then create multiple tests:
test1 = Test(1)
test2 = Test(2)
test3 = Test(3)
instance = Test.get(3)
# [<__main__.Test object at 0x03F29CD0>]
instance[0].value
# 3
It makes sense for me to return a list of instances instead of one single instance. If you however is only interested in the first match, you can modify the return statement accordingly.
Original answer:
instance = Test.get('foobar') is the problem. You're referencing Test by its class, not its instance. So naturally the instance method .get(self, value) is looking for the first argument self for the instance.
Usually if you already have an instance (e.g. Test().get('foobar')) then the instance is passed into the instance method as self by default.
You could still call the instance method, but you just need to explicitly pass the instance in:
Test.get(test, 'foobar')
The get method belongs to the instance, not the class. A class does not know its instances (you could do some dirty tricks to let the class know, but don't). What you need is a list of instances.
One way you could do it
override the equality operator
create a list of the instances
So, something like
class Test:
def __eq__(self, value):
return self.value == value
test_list = [Test(5), Test(2), Test(3)]
position = test_list.index(2)
The "get" method of your class is not a class method therefore you have to call it on the instance:
test.get("foobar")
However if you modify the method to a classmethod than it won't be able to access the instance attributes only the class atributes will be visible.
For a quick solution I think you should store all of the instances in a datastructure (for example in a list) and you can create a function which will loop through the instances and returns the correct one.

Access a class property by both class name and parent class name

Consider this snippet:
def populate(l):
l.append(1)
class First:
__obj__ = []
class Second(First):
populate(First.__obj__)
def __init__(self):
pass
def __call__(self):
for i in Second.__obj__:
print(i)
a = Second()
a()
When run, it will output 1, so it seems that First.__obj___ and Second.__obj__ point to the same object. Is it true and how does class property work in the case of inheritance?
The name __obj__ is an attribute of the First class. Because the Second class inherits the First class, it also inherits the class attribute __obj__. Both First.__obj__ and Second.__obj__ are referencing the same list object. An instance's self.__obj__ also references the same object.
This is because lists are mutable. This would not be the case with an immutable type like str.

Python Instantiate Class Within Class Definition

I am attempting to add a variable to a class that holds instances to the class. The following is a shortened version of my code.
class Classy :
def __init__(self) :
self.hi = "HI!"
# "CLASSIES" variable holds instances of class "Classy"
CLASSIES = []
for i in xrange(0,4) :
CLASSIES.append(Classy())
Upon running the code, I get the following error.
Traceback (most recent call last):
File "classy.py", line 6, in Classy
CLASSIES.append(Classy())
NameError: name 'Classy' is not defined
Is there another way to add instances of a class to a class/static variable within that class?
The class body is executed before the class is created. Therefore, you are attempting the instantiate the class before it exists. You can still attach instances to the class, but you have to create them after the class body finished, e.g.:
class Classy(object):
def __init__(self):
self.hi = "HI!"
CLASSIES = []
for i in xrange(4):
Classy.CLASSIES.append(Classy())
However, I'd suggest you first think long and hard whether you actually need this effectively-global list, and whether you need it to be part of the class object. Personally, I almost never do something like this.
The simplest way to do this is do it after the class is created, when the class has been defined, and therefore can be used:
class Classy :
CLASSIES = []
def __init__(self) :
self.hi = "HI!"
Classy.CLASSIES = [Classy() for _ in xrange(0,4)]
(Here using a list comprehension for convinience, as it's the most readable and efficent way to build a list).
Also note that if this intended to be a constant, you should probably make it a tuple rather than a list, and if it isn't intended to be, you should probably not use an ALL_CAPS name which, by convention, implies a constant.
It seems to me that you want to obtain that:
class Classy :
CLASSIES = []
def __init__(self) :
self.hi = "HI!"
Classy.CLASSIES.append(self)
for i in xrange(4):
Classy()
for x in Classy.CLASSIES:
print x
result
<__main__.Classy instance at 0x011DF3F0>
<__main__.Classy instance at 0x011DF440>
<__main__.Classy instance at 0x011DF418>
<__main__.Classy instance at 0x011DF2B0>
EDIT
Note that with the code of Lattyware:
class Classy :
CLASSIES = []
idC = id(CLASSIES)
def __init__(self) :
self.hi = "HI!"
#Classy.CLASSIES.append(self)
Classy.CLASSIES = [Classy() for _ in xrange(0,4)]
print Classy.idC
print id(Classy.CLASSIES)
print 'Classy.idC==id(Classy.CLASSIES) :',Classy.idC==id(Classy.CLASSIES)
result
18713576
10755928
Classy.idC==id(Classy.CLASSIES) : False
While with the for loop of delnan'code, it doesn't appear.
However it's easy to correct:
writing
Classy.CLASSIES[:] = [Classy() for _ in xrange(0,4)]
or
Classy.CLASSIES.extend(Classy() for _ in xrange(0,4))
instead of
Classy.CLASSIES = [Classy() for _ in xrange(0,4)]
it depends of what is desired.
EDIT 2
Methods may reference global names in the same way as ordinary
functions. The global scope associated with a method is the module
containing its definition. (A class is never used as a global scope.)
http://docs.python.org/2/tutorial/classes.html#class-definition-syntax
A class has a namespace implemented by a dictionary object. Class
attribute references are translated to lookups in this dictionary,
e.g., C.x is translated to C.__dict__["x"]
http://docs.python.org/2/reference/datamodel.html#new-style-and-classic-classes
class Classy :
CLASSIES = []
print '"CLASSIES" in globals()',"CLASSIES" in globals()
print '"CLASSIES" in Classy.__dict__ ==',"CLASSIES" in Classy.__dict__
result
"CLASSIES" in globals() False
"CLASSIES" in Classy.__dict__ == True
Delnan, how will you continue to pretend that CLASSIES is global ??
Did I misunderstood something in your debate with Lattyware ?
The class itself is not defined until after the class block finishes executing, so you can't make use of the class inside its own definition.
You could use a class decorator or a metaclass to add your desired class variable after the class is created. Here's an example with a decorator.
def addClassy(cls):
cls.CLASSIES = [cls() for a in xrange(4)]
return cls
#addClassy
class Classy(object):
pass
>>> Classy.CLASSIES
0: [<__main__.Classy object at 0x000000000289A240>,
<__main__.Classy object at 0x000000000289A518>,
<__main__.Classy object at 0x000000000289A198>,
<__main__.Classy object at 0x000000000289A208>]

Is it safe to replace a self object by another object of the same type in a method?

I would like to replace an object instance by another instance inside a method like this:
class A:
def method1(self):
self = func(self)
The object is retrieved from a database.
It is unlikely that replacing the 'self' variable will accomplish whatever you're trying to do, that couldn't just be accomplished by storing the result of func(self) in a different variable. 'self' is effectively a local variable only defined for the duration of the method call, used to pass in the instance of the class which is being operated upon. Replacing self will not actually replace references to the original instance of the class held by other objects, nor will it create a lasting reference to the new instance which was assigned to it.
As far as I understand, If you are trying to replace the current object with another object of same type (assuming func won't change the object type) from an member function. I think this will achieve that:
class A:
def method1(self):
newObj = func(self)
self.__dict__.update(newObj.__dict__)
It is not a direct answer to the question, but in the posts below there's a solution for what amirouche tried to do:
Python object conversion
Can I dynamically convert an instance of one class to another?
And here's working code sample (Python 3.2.5).
class Men:
def __init__(self, name):
self.name = name
def who_are_you(self):
print("I'm a men! My name is " + self.name)
def cast_to(self, sex, name):
self.__class__ = sex
self.name = name
def method_unique_to_men(self):
print('I made The Matrix')
class Women:
def __init__(self, name):
self.name = name
def who_are_you(self):
print("I'm a women! My name is " + self.name)
def cast_to(self, sex, name):
self.__class__ = sex
self.name = name
def method_unique_to_women(self):
print('I made Cloud Atlas')
men = Men('Larry')
men.who_are_you()
#>>> I'm a men! My name is Larry
men.method_unique_to_men()
#>>> I made The Matrix
men.cast_to(Women, 'Lana')
men.who_are_you()
#>>> I'm a women! My name is Lana
men.method_unique_to_women()
#>>> I made Cloud Atlas
Note the self.__class__ and not self.__class__.__name__. I.e. this technique not only replaces class name, but actually converts an instance of a class (at least both of them have same id()). Also, 1) I don't know whether it is "safe to replace a self object by another object of the same type in [an object own] method"; 2) it works with different types of objects, not only with ones that are of the same type; 3) it works not exactly like amirouche wanted: you can't init class like Class(args), only Class() (I'm not a pro and can't answer why it's like this).
Yes, all that will happen is that you won't be able to reference the current instance of your class A (unless you set another variable to self before you change it.) I wouldn't recommend it though, it makes for less readable code.
Note that you're only changing a variable, just like any other. Doing self = 123 is the same as doing abc = 123. self is only a reference to the current instance within the method. You can't change your instance by setting self.
What func(self) should do is to change the variables of your instance:
def func(obj):
obj.var_a = 123
obj.var_b = 'abc'
Then do this:
class A:
def method1(self):
func(self) # No need to assign self here
In many cases, a good way to achieve what you want is to call __init__ again. For example:
class MyList(list):
def trim(self,n):
self.__init__(self[:-n])
x = MyList([1,2,3,4])
x.trim(2)
assert type(x) == MyList
assert x == [1,2]
Note that this comes with a few assumptions such as the all that you want to change about the object being set in __init__. Also beware that this could cause problems with inheriting classes that redefine __init__ in an incompatible manner.
Yes, there is nothing wrong with this. Haters gonna hate. (Looking at you Pycharm with your in most cases imaginable, there's no point in such reassignment and it indicates an error).
A situation where you could do this is:
some_method(self, ...):
...
if(some_condition):
self = self.some_other_method()
...
return ...
Sure, you could start the method body by reassigning self to some other variable, but if you wouldn't normally do that with other parametres, why do it with self?
One can use the self assignment in a method, to change the class of instance to a derived class.
Of course one could assign it to a new object, but then the use of the new object ripples through the rest of code in the method. Reassiging it to self, leaves the rest of the method untouched.
class aclass:
def methodA(self):
...
if condition:
self = replace_by_derived(self)
# self is now referencing to an instance of a derived class
# with probably the same values for its data attributes
# all code here remains untouched
...
self.methodB() # calls the methodB of derivedclass is condition is True
...
def methodB(self):
# methodB of class aclass
...
class derivedclass(aclass):
def methodB(self):
#methodB of class derivedclass
...
But apart from such a special use case, I don't see any advantages to replace self.
You can make the instance a singleton element of the class
and mark the methods with #classmethod.
from enum import IntEnum
from collections import namedtuple
class kind(IntEnum):
circle = 1
square = 2
def attr(y): return [getattr(y, x) for x in 'k l b u r'.split()]
class Shape(namedtuple('Shape', 'k,l,b,u,r')):
self = None
#classmethod
def __repr__(cls):
return "<Shape({},{},{},{},{}) object at {}>".format(
*(attr(cls.self)+[id(cls.self)]))
#classmethod
def transform(cls, func):
cls.self = cls.self._replace(**func(cls.self))
Shape.self = Shape(k=1, l=2, b=3, u=4, r=5)
s = Shape.self
def nextkind(self):
return {'k': self.k+1}
print(repr(s)) # <Shape(1,2,3,4,5) object at 139766656561792>
s.transform(nextkind)
print(repr(s)) # <Shape(2,2,3,4,5) object at 139766656561888>

Categories

Resources