Questions tagged [class-attributes]

170 questions
3
votes
1 answer

Why class level attributes work with Null

I would think the following would throw NullPointerException class N { static int i; public static void main( String ... args ) { System.out.println( ((N)null).i ); } } But it doesn't. Why?
OscarRyz
  • 196,001
  • 113
  • 385
  • 569
3
votes
1 answer

how to move function to class? python

I would like to to use service functions in a classmethod, where the service function is defined somewhere else. I want to be dynamic, so I can define different functions in different situations. I've tried this: def print_a(): print 'a' class…
3
votes
2 answers

Python design - initializing, setting, and getting class attributes

I have a class in which a method first needs to verify that an attribute is present and otherwise call a function to compute it. Then, ensuring that the attribute is not None, it performs some operations with it. I can see two slightly different…
3
votes
2 answers

Lifetime of Python class attribute

What's the lifetime of a class attribute, in Python? If no instances of the class are currently live, might the class and its class attributes be garbage-collected, and then created anew when the class is next used? For example, consider something…
D.W.
  • 3,382
  • 7
  • 44
  • 110
3
votes
1 answer

Inheritable attributes in ruby classes

Greets to all! I want to describe each kind of product by a class: # Base product class class BaseProduct prop :name, :price # Common properties for all inheritable products end class Cellphone < BaseProduct prop :imei # Concrete property for…
rbnoob
  • 99
  • 1
  • 8
3
votes
2 answers

Python - Referencing class name from inside class body

In Python, I want to have a class attribute, a dictionary, with initialized values. I wrote this code: class MetaDataElement: (MD_INVALID, MD_CATEGORY, MD_TAG) = range(3) mapInitiator2Type = {'!':MetaDataElement.MD_CATEGORY, …
redism
  • 500
  • 7
  • 18
3
votes
2 answers

Python shorthand for a tuple of a set of class attributes

Say i am using a class from some python package that looks like the following class foo(object): def __init__(self): self.a = None self.b = None self.c = None self.d = None self.e = None self.f =…
wich
  • 16,709
  • 6
  • 47
  • 72
2
votes
3 answers

why superclass attributes are not available in the current class' namespace?

Example: class A: a = 1 class B(A): b = 2 y = b # works fine x = a # NameError: name 'a' is not defined x = A.a # works fine z = B() z.a # works fine B.a # works fine Why is x = a not allowed? In every other context (access…
max
  • 49,282
  • 56
  • 208
  • 355
2
votes
4 answers

When does Python fall back onto class __dict__ from instance __dict__?

Please see the below snippet: class Foo: class_var = "hi" foo = Foo() assert foo.class_var is Foo.class_var assert "class_var" in Foo.__dict__ assert "class_var" not in foo.__dict__ All assertions here pass, though I am not sure if it's…
Intrastellar Explorer
  • 3,005
  • 9
  • 52
  • 119
2
votes
2 answers

How to show that which class's variables are used in which class's operation(s) using UML diagram?

There is a class diagram that contains one class and this class has three operations (Oper1, Oper2, and Oper3) and three attributes (Attr1, Attr2, Atrr3). Now suppose: 1- Oper1 uses Attr1 and Attr2 to read from and write to them, 2- Oper2 uses Attr3…
Anmk
  • 193
  • 1
  • 9
2
votes
2 answers

Redirecting function definitions in python

This is a very contrived example as it's not easy to explain the context in which I have ended up implementing this solution. However, if anyone can answer why this particular peculiarity happens, I'd be grateful. The example: class A(dict): …
Dan
  • 33,953
  • 24
  • 61
  • 87
2
votes
4 answers

How can i add atrributes to the class method from outside class?

This is gonna be my first question on this website. If I do mistake about English, sorry. Okey, my question is how can I add or set attribute to the class method from outside class? If i am not wrong,we use settatr() to do this. Can you guys help me…
Berke Balcı
  • 81
  • 10
2
votes
1 answer

Access class level attribute of another class?

I can access class-level attribute of a class (B) in method of another class (A) but I cannot access that at class-level of class A. The issue can be resolved by defining B after A but why is this so and how can I access that keeping class B after…
ahsan naeem
  • 245
  • 2
  • 9
2
votes
1 answer

how to easily find a class instance in a list with a given class attribute?

I'm a newbie. I am wondering if there is a way to easily find an instance of a class in a list with a given attribute. (Python) basically the set up is as follows: class idk: def __init__(self, name, num): self.name = name …
2
votes
2 answers

Python using class attributes with super when inheriting

I have a piece of python code: class A(object): args = [1,2] def __init__(self): print self.args class B(A): args = [3,4] def __init__(self): super(B, self).__init__() print self.args B() The output is: [3,…
jerrypeng
  • 104
  • 1
  • 5