This question already has answers here:
Can I dynamically convert an instance of one class to another?
(5 answers)
Assigning to an instance's __class__ attribute in Python
(1 answer)
Closed 4 years ago.
Is it possible to change the class of an object? In the following example:
class Fisch:
...
class Trout(Fish):
def some_special_trout_method:
...
...
class Salmon(Fish):
...
...
fisch_object = Trout()
Would it be possible to change the trout into a salmon and keep the class variable values?
If this is possible how? And if it's possible, is it a good idea?
Related
This question already has answers here:
Why does updating a class attribute not update all instances of the class?
(6 answers)
Why isn't my class variable changed for all instances?
(3 answers)
Closed 5 months ago.
class Dog:
breed='German'
def __init__(self,name,age):
self.name=name
self.age=age
d1=Dog('fluffy',10)
d2=Dog('pookie',5)
d1.breed='Pitbull'
print(d1.breed)
print(d2.breed)
output-
Pitbull
German
I created a class variable breed and later on i tried to change the value of it by using the instance d1 and as we know that only single copy of class variable is available so effect should have been visible in all the copies but the same does not hold true here,why?
This question already has answers here:
obtaining named attributes of self
(2 answers)
Closed 3 years ago.
I want to know how can I access a class attribute using a string. E.g
class Test:
def __init__(self):
# defined self.name here
self.name.person = 1
If I have the program:
a = "person"
b = Test()
How can I print self.name.person using the variable a?
print(b.name.a)
Many thanks!
Python has getattr() as built-in function:
print(getattr(b.name, a))
This question already has answers here:
How do I get the string with name of a class?
(5 answers)
Closed 4 years ago.
class A:
pass
> A().__class__.__name__
-->'A'
> A.__class__.__name__
--> 'type'
How can I get 'A' without the parens?
You're getting 'type', because the class of a class definition is... type (in other words: a class definition is a type).
You can just use the __name__ attribute. No need to look for the __class__, you already have the class:
A.__name__
'A'
Just to make it completely clear:
A().__class__ is A
True
This question already has answers here:
Can you monkey patch methods on core types in Python?
(15 answers)
Closed 8 years ago.
"this is my string".myfunction(argument)
This is very simple in javascript. With the keyword this i can access to my string directly. Is that possible with python?
You can inherit from str and define your own methods:
class myString(str):
def my_method(self, ...):
# ...
some_string = myString("StackOverflow")
print some_string.count("a") # method from string
print some_string.myMethod(...) # your defined method
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